天天看点

mybatis-使用where动态拼接sql

一、创建项目和数据库

    项目名称:mybatis092901

    数据库名称:mybatis0929

        表名称:dept

        CREATE TABLE `dept` (

          `deptNo` int(11) NOT NULL,

          `deptName` varchar(30) DEFAULT NULL,

          `location` varchar(30) DEFAULT NULL,

          PRIMARY KEY (`deptNo`)

        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

        表名称:emp

        CREATE TABLE `emp` (

          `empno` int(11) NOT NULL,

          `ename` varchar(30) DEFAULT NULL,

          `hiredate` date DEFAULT NULL,

          `job` varchar(30) DEFAULT NULL,

          `sal` double DEFAULT NULL,

          `mgr` varchar(30) DEFAULT NULL,

          `comm` varchar(30) DEFAULT NULL,

          `deptno` int(11) DEFAULT NULL,

          PRIMARY KEY (`empno`)

二、添加jar包

    1.在项目上创建lib目录

        /lib

    2.在lib目录下添加jar

        junit-4.10.jar

        mybatis-3.2.2.jar

        mysql-connector-java-5.1.10-bin.jar

三、添加配置文件

    1.在项目上创建conf目录

        /conf

    2.在conf目录下添加配置文件

        配置文件名称: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 alias="Emp" type="cn.jbit.mybatis092901.domain.Emp"/>

            </typeAliases>

            <environments default="development">

                <environment id="development">

                <transactionManager type="JDBC"/>

                    <dataSource type="POOLED">

                    <property name="driver" value="com.mysql.jdbc.Driver"/>

                    <property name="url" value="jdbc:mysql://localhost:3306/mybatis0929"/>

                    <property name="username" value="root"/>

                    <property name="password" value="root"/>

                    </dataSource>

                </environment>

            </environments>

        </configuration>

四、创建实体类

    1.在src目录下创建包

        包名:cn.jbit.mybatis092901.domain

    2.在包下创建实体类

        类名:Dept.java

        内容:

        public class Dept implements Serializable {

            private Integer deptNo;//部门编号

            private String deptName;//部门名称

            private String location;//部门地址

            //省略get and set 

        }

        类名:Emp.java

        public class Emp implements Serializable {

            //员工姓名

            private String empName;

            //员工编号

            private Integer empNo;

            //员工入职时间

            private Date hireDate;    

            //员工职位

            private String job;

            //员工工资

            private Double salary;

            //经理编号

            private Integer mgr;

            //奖金

            private Double comm;

            //部门编号

            private Integer deptNo;

五、持久层设计

    1.接口设计

        1).在src目录创建包

            包名:cn.jbit.mybatis092901.dao

        2).在包下创建接口

            接口名:IEmpDao.java

            接口中内容:

            public interface IEmpDao {

                /**

                 * 使用where

                 * @param emp

                 * @return

                 */

                public List<Emp> findEmpByExampleWhere(Emp emp);

            }

六、添加相关映射文件

    1.在conf下添加配置文件

        映射文件名称:EmpDaoMapper.xml

        映射文件内容:

        <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 

        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

        <mapper namespace="cn.jbit.mybatis092901.dao.IEmpDao">

            <resultMap id="empResultMap" type="cn.jbit.mybatis092901.domain.Emp">

                <id property="empNo" column="empno" />

                <result property="empName" column="ename" />

                <result property="hireDate" column="hiredate" />

                <result property="job" column="job" />

                <result property="salary" column="sal" />

                <result property="comm" column="comm" />

            </resultMap>

            <!--

                使用where进行动态Sql拼接 

            -->

            <select id="findEmpByExampleWhere" parameterType="Emp" resultMap="empResultMap">

                  SELECT * FROM EMP

                  <where>

                    <if test ="deptNo != null">

                      and deptno= #{deptNo}

                    </if>

                    <if test ="empName != null">

                      and ename like #{empName}

                  </where>

              </select>

        </mapper>

七.持久层实现类设计

        1).在src目录下创建包

            包名:cn.jbit.mybatis092901.dao.impl

        2).在包下创建实现类

            实现类名:EmpDaoImpl.java

            实现类中的内容:

            /**

             * 为了使用代码看起来清晰,SqlSession的获取都使用工具类

             */

            @Override

            public void insertEmp(Emp emp) {

                String resource = "mybatis-config.xml";

                Reader reader = null;

                SqlSessionFactory factory = null;

                SqlSession session = null;

                try {

                    reader = Resources.getResourceAsReader(resource);

                    SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

                    factory = builder.build(reader);

                    session = factory.openSession();

                    List<Emp> empList = new ArrayList();

                    empList = session.selectList("cn.jbit.mybatis092901.dao.IEmpDao.findEmpByExampleIf",emp);

                } catch (Exception e1) {

                    e1.printStackTrace();

                }finally {

                    session.close();

                }

八、在配置文件中添加映射文件引用

    1.在mybatis-fonfig.xml文件中添加mapper

        <mappers>

            <mapper resource="EmpDaoMapper.xml"/>

        </mappers>

九、测试操作

    1.在项目中创建test目录

        /test

    2.在test目录下创建包

        cn.jbit.mybatis092901.dao

    3.在包下创建测试类

        类名:EmpDaoTest.java

        public class EmpDaoTest {

            //员工类的持久层实现

            private static IEmpDao empDao = new EmpDaoImpl();

             * 测试where

            @Test

            public void testwhere(){

                Emp emp = new Emp();

                emp.setDeptNo(9);

                emp.setEmpName("张平U");

                List<Emp> emps = empDao.findEmpByExampleWhere(emp);

                printEmpList(emps);

            }        

            private void printEmpList(List<Emp> empList){

                for(Emp emp:empList){

                    printEmp(emp);

本文转自  素颜猪  51CTO博客,原文链接:http://blog.51cto.com/suyanzhu/1559319