MyBatis配置 增 删 改 查 带条件查询 模糊查询

1.下载jar

 

http://repo1.maven.org/maven2/org/mybatis/mybatis/

 

2.附加jar

 

3.编写MyBatis配置文件 mybatis-config.xml

<?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>
    <!-- 别名定制 -->
    <typeAliases >
        <typeAlias type="cn.happy.entity.Dept" alias="Dept"/>
    </typeAliases>
    
    <environments default="development">
        <environment id="development">
        <!-- 数据源的方式 POOLED-->
            <transactionManager type="JDBC" />
             <dataSource type="POOLED">
                <property name="driver" value="oracle.jdbc.OracleDriver" />
                <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
                <property name="username" value="lrz" />
                <property name="password" value="lrz" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
    <mapper resource="cn/happy/entity/Dept.xml"></mapper>
    <mapper resource="cn/happy/entity/GetMapper.xml"></mapper>    
    </mappers>
</configuration>

 

4.创建实体类Dept

package cn.happy.entity;

public class Dept {
    private Integer deptNo;
    private String deptName;
    public Integer getDeptNo() {
        return deptNo;
    }
    public void setDeptNo(Integer deptNo) {
        this.deptNo = deptNo;
    }
    public String getDeptName() {
        return deptName;
    }
    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }
    @Override
    public String toString() {
        return "Dept [deptNo=" + deptNo + ", deptName=" + deptName + "]";
    }
    
    
}

 

5.创建SQL映射文件 Dept.xml

 

<?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.happy.entity.Dept">
<!-- id:唯一标识:通过此id,程序可唯一锁定一条SQL 
     parameterType:参数类型
     resultType:结果类型
-->

    <!-- 01.select -->
    <select id="selectAllDept" resultType="Dept">
        select * from Dept
    </select>
    
    <!-- 01.selectById -->
    <select id="selectById" resultType="Dept">
        select * from Dept where deptNo=#{deptNo}
    </select>
    
    <!-- 02.insert -->
    <insert id="insertDept" parameterType="Dept">
        insert into Dept values(#{deptNo},#{deptName})
    </insert>
    
    <!-- 03.update  -->
    <update id="updateDept" parameterType="Dept">
        update Dept set deptName=#{deptName} where deptNo=#{deptNo}
    </update>
    
    <!-- 04.delete  -->
    <delete id="deleteDept" parameterType="Dept">
        delete from Dept where deptNo=#{deptNo}
    </delete>
    
    <!-- 04.like  -->
    <select id="selectLikeDept" parameterType="Dept" resultType="Dept">
        select * from Dept where deptName like '%${deptName}%'
    </select>
    
    
</mapper>

 

 

6.书写测试类

public class MybatisTest {
    
    SqlSession session ;
    
    @Before
    public void initData() throws Exception{
        String resource="mybatis-config.xml";
        //输入流
        Reader reader = Resources.getResourceAsReader(resource);
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(reader);
        session = factory.openSession();
        //将一个字符串和硬盘上的真实文件关联 , 在内存形成流
        
    }
    
    
    /**
     * 01.查找所有部门
     * 
     */
    @Test
    public void testSelect() throws Exception{
        List<Dept> list = session.selectList("cn.happy.entity.Dept.selectAllDept");
        for (Dept item : list) {
            System.out.println(item);
        }
    }
    
    
    /**
     * 01.带条件查询部门
     * 
     */
    @Test
    public void testSelectById() {
        Dept dt=new Dept();
        dt.setDeptNo(2);

        Dept dept = session.selectOne("cn.happy.entity.Dept.selectById",dt);
         System.out.println(dept.getDeptName());
    }
    
    /**
     * 02.新增部门
     * 
     */
    @Test
    public void testInsert() throws Exception{
        Dept dept=new Dept();
        dept.setDeptNo(1);
        dept.setDeptName("小弟部");

        int count = session.insert("cn.happy.entity.Dept.insertDept",dept);
        
        //提交
        session.commit();
        
        System.out.println(count);
    }
    
    
    /**
     * 03.修改部门
     * 
     */
    @Test
    public void testUpdate() throws Exception{
        Dept dept=new Dept();
        dept.setDeptNo(1);
        dept.setDeptName("小弟部1");
        
        int count = session.update("cn.happy.entity.Dept.updateDept",dept);
        
        //提交
        session.commit();
        
        System.out.println(count);
    }
    
    
    /**
     * 04.删除部门
     * 
     */
    @Test
    public void testDelete() throws Exception{
        Dept dept=new Dept();
        dept.setDeptNo(1);
        
        int count = session.delete("cn.happy.entity.Dept.deleteDept",dept);
        
        //提交
        session.commit();
        
        System.out.println(count);
    }
    
    
    
    /**
     * 04.模糊查询部门
     * 
     */
    @Test
    public void testSelectLike() throws Exception{
        Dept dept=new Dept();
        dept.setDeptName("大神");
        
        List<Dept> list = session.selectList("cn.happy.entity.Dept.selectLikeDept",dept);
        
        for (Dept item : list) {
            System.out.println(item);
        }
        
    }
    
    /**
     * 01.查找所有部门
     * 
     */
    @Test
    public void testGetAllDepts() throws Exception{
        IDeptDao mapper = session.getMapper(IDeptDao.class);
        List<Dept> list = mapper.getAllDepts();
        for (Dept item : list) {
            System.out.println(item.getDeptName());
        }
    }
    
}

 

posted on 2016-09-18 15:15  -EASY-  阅读(5556)  评论(0编辑  收藏  举报

导航