MyBatis

来源:互联网 发布:宁波plc编程招聘 编辑:程序博客网 时间:2024/05/22 12:45

创建数据库(y2165)


MyBatis环境搭建

1.在pom.xml引入依赖

2.得替换build节点,为了让程序编译在main中所有子包下的配置文件

3.构建大配置,位于resources

<?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"><!-- 通过这个配置文件,完成mybatis与数据库的连接 --><configuration><!--根节点-->    <!--别名设定-->     <typeAliases>         <package name="cn.happy.entity"/>     </typeAliases>    <environments default="development">        <environment id="development">            <!--           transactionManager:JDBC保证事务的           update           delete           事务分类:JDBC:编程式事务                       xxx.beginTransaction()                       tx.commit()                       tx.rollback()                     配置式事务                     JDBC|MANAGED                     区别           -->            <!-- 配置事物管理采用JDBC  -->            <transactionManager type="JDBC"/>            <!--               POOLED:MyBatis内置的连接池               c3p0连接池                       POOLED 、UNPOOLED  、JNDI           -->            <!-- POOLED:mybatis的数据源,JNDI:基于tomcat的数据源 -->            <dataSource type="POOLED">                <property name="driver" value="com.mysql.jdbc.Driver"/>                <property name="url" value="jdbc:mysql:///y2165"/>                <property name="username" value="cd"/>                <property name="password" value="000"/>            </dataSource>        </environment>    </environments>    <mappers>        <mapper resource="cn/happy/dao/IDeptDAO.xml"/>    </mappers></configuration>


4.构建实体类

package cn.happy.entity;/** * Created by lenovo on 2017/7/7. */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;    }}


5.构建小配置 

<?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 "><!--namespace:命名空间:区分不同空间下的同名SQLIDA: findlAllB:  findAll--><mapper namespace="cn.happy.dao.IDeptDAO" >    <!--SQL标签     id:唯一锁定到SQL标识     paramenterType:SQL语句的入参  可以省略     resultType:     增删除操作:不能 写     查询:单个实体的类型   -->    <!--查询所有-->    <select id="getAllList" resultType="Dept">        SELECT * FROM Dept    </select>    <!--待条件查询-->    <select id="getDeptById" parameterType="int" resultType="Dept">        select * from Dept where deptNo=#{deptNo}    </select></mapper>


6.在大配置中关联小配置

<mappers>        <mapper resource="cn/happy/dao/IDeptDAO.xml"/>    </mappers>


7.书写测试类

package cn.happy.test;import cn.happy.dao.IDeptDAO;import cn.happy.entity.Dept;import org.apache.ibatis.io.Resources;import org.apache.ibatis.jdbc.SQL;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.Test;import java.io.IOException;import java.io.InputStream;import java.util.List;/** * Created by lenovo on 2017/7/9. */public class MyBatisTest01 {    //单元测试    @Test    //1.查询所有使用getMapper()    public void testall() {        //1 获取到大配置        String path = "mybatis-config.xml";        try {            InputStream is = Resources.getResourceAsStream(path);            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);            SqlSession session = factory.openSession();            IDeptDAO de = session.getMapper(IDeptDAO.class);            List<Dept> list = de.getAllList();            for (Dept dept : list) {                System.out.println(dept.getDeptName());            }        } catch (IOException e) {            e.printStackTrace();        }    }    @Test    //2.待条件查询  Mapper的使用    public void testGetOneDept() {        String path = "mybatis-config.xml";        try {            InputStream is = Resources.getResourceAsStream(path);            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);            SqlSession session = factory.openSession();            IDeptDAO dao = session.getMapper(IDeptDAO.class);            Dept dept = dao.getDeptById(1);            System.out.println(dept.getDeptName());        } catch (IOException e) {            e.printStackTrace();        }    }


使用getMapper查询所有结果


使用getMapper带条件查询结果




原创粉丝点击