MyBatis初学者配置

时间:2020-12-17 16:16:18

小配置

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.entity.Dept">
<!-- id:唯一标识:通过此id,程序可唯一锁定一条SQL
parameterType:参数类型
resultType:结果类型
-->
<sql id="columns">
deptid,deptname
</sql>
<!-- 查询部分列*号用别名 -->
<select id="selectSql" resultType="cn.entity.Dept">
select <include refid="columns"></include> from Dept
</select> <resultMap id="Dept" type="cn.entity.Dept">
<result property="deptid" column="deptid"></result>
</resultMap>
<!-- 查询所有部门信息 -->
<select id="selectDept" resultMap="Dept">
select * from Dept
</select>
<!-- 带条件查询 -->
<select id="selectDeptById" parameterType="cn.entity.Dept" resultType="cn.entity.Dept">
select * from Dept where deptid=#{deptid}
</select>
<!-- 添加部门信息 -->
<insert id="insertDept" parameterType="cn.entity.Dept">
insert into Dept(deptname) values(#{deptname})
</insert> <!-- 删除部门信息 -->
<delete id="deleteDept" parameterType="cn.entity.Dept">
delete from Dept where deptid=#{deptid}
</delete>
<!-- 修改部门信息 -->
<update id="updateDept" parameterType="cn.entity.Dept">
update Dept set deptname=#{deptname} where deptid=#{deptid}
</update>
<!-- 模糊查询 -->
<select id="likeDept" resultType="cn.entity.Dept" parameterType="cn.entity.Dept">
select * from Dept where deptname like '%${deptname}%'
</select>
<!-- 智能标签where 动态查询 -->
<select id="selectDeptDynamic" parameterType="cn.entity.Dept" resultType="cn.entity.Dept">
select * from Dept
<where>
<if test="deptid!=null">
and deptid=#{deptid}
</if>
<if test="deptname!=null">
and deptname=#{deptname}
</if>
</where>
</select>
<!-- 智能标签set更新 -->
<update id="updatesetDept" parameterType="cn.entity.Dept" >
update Dept
<set>
<if test="deptid!=null">deptid=#{deptid},</if>
<if test="deptname!=null">deptname=#{deptname},</if>
</set>
where deptid=#{deptid}
</update> </mapper>

大配置

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<!-- 事务策略是JDBC -->
<transactionManager type="JDBC" />
<!-- 数据源的方式 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://120.25.78.77:3306/zc" />
<property name="username" value="zc" />
<property name="password" value="zc" />
</dataSource>
</environment>
</environments>
<!--映射文件:描述某个实体和数据库表的对应关系 -->
<mappers>
<mapper resource="cn/entity/Dept.xml" />
</mappers>
</configuration>

测试类

 package cn.test;

 import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.List; import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test; import cn.entity.Dept; public class MybatisTest { SqlSession session;
@Before
public void initData() throws FileNotFoundException{
//1.1 SqlSessionFactoryBuilder
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder(); //1.2 SqlSesssionFactory
Reader reader=new FileReader("src/mybatis-config.xml");
SqlSessionFactory factory = builder.build(reader); //1.3 SqlSession
session= factory.openSession();
}
/**
* 查询所有部门所有信息
* @throws IOException
*/
@Test
public void selectDeptTest() throws IOException
{
List<Dept> list = session.selectList("selectDept");
for (Dept dept : list) {
System.out.println(dept.getDeptname());
}
session.close();
}
/**
* 增加
*/
@Test
public void insertDeptTest()
{
Dept dt=new Dept();
dt.setDeptname("大牛部");
int count =session.insert("insertDept",dt);
System.out.println(count);
session.close(); }
/**
* 带条件查询
*/
@Test
public void selectDeptByIdTest()
{
Dept dt= new Dept();
dt.setDeptid(1);
Dept dept=session.selectOne("selectDeptById",dt);
System.out.println(dept.getDeptname());
session.close(); }
/**
* 删除
*/
@Test
public void deleteDeptTest()
{
Dept dt=new Dept();
dt.setDeptid(3);
int count = session.delete("deleteDept",dt);
session.commit();
System.out.println(count);
session.close();
}
/**
* 修改
*/
@Test
public void updateDeptTest()
{
Dept dt=new Dept();
dt.setDeptid(1);
dt.setDeptname("公关部");
int count = session.update("updateDept",dt);
session.commit();
System.out.println(count);
session.close();
}
/**
* 模糊查询
*
*/
@Test
public void likeDeptTest()
{
Dept dt=new Dept();
dt.setDeptname("公");
List<Dept> list = session.selectList("likeDept",dt);
for (Dept dept : list) {
System.out.println(dept.getDeptname());
}
session.close(); }
/**
* 别名查询
*/
@Test
public void selectSqlTest() {
List<Dept> list = session.selectList("selectSql");
for (Dept dept : list) {
System.out.println(dept.getDeptname());
}
session.close(); }
/**
* 智能标签where进行动态查询
*/ @Test
public void dynamicWhereTest() throws Exception{
Dept dept=new Dept();
//代表前台用户同时使用 部门编号 和 部门名称 发起了 检索
dept.setDeptname("公关部");
dept.setDeptid(1); List<Dept> list=session.selectList("selectDeptDynamic",dept);
for (Dept dt : list) {
System.out.println(dt.getDeptname());
}
session.close();
}
/**
* 智能标签set更新
*
*/
@Test
public void updatesetDeptTest()
{
Dept dt=new Dept();
dt.setDeptid(1);
dt.setDeptname("公布部");
int count = session.update("updatesetDept",dt);
session.commit();
System.out.println(count);
session.close();
} }