[关闭]
@Beeder 2018-02-23T21:17:38.000000Z 字数 15707 阅读 562

Mybatis框架

javaWeb Mybatis


导入jar包

  1. asm-3.3.1.jar
  2. cglib-2.2.2.jar
  3. commons-logging-1.1.1.jar
  4. javassist-3.17.1-GA.jar
  5. log4j-1.2.17.jar
  6. log4j-api-2.0-rc1.jar
  7. log4j-core-2.0-rc1.jar
  8. slf4j-api-1.7.5.jar
  9. slf4j-log4j12-1.7.5.jar

操作流程

基本操作

log4j.properties

  1. # Global logging configuration
  2. log4j.rootLogger=DEBUG, stdout
  3. # Console output...
  4. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  5. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  6. log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

db.properties

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
  3. jdbc.username=root
  4. jdbc.password=admin

SqlMapConfig.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <properties resource="db.properties"></properties>
  7. <environments default="development">
  8. <environment id="development">
  9. <!-- 使用jdbc事务管理-->
  10. <transactionManager type="JDBC" />
  11. <!-- 数据库连接池-->
  12. <dataSource type="POOLED">
  13. <property name="driver" value="${jdbc.driver}" />
  14. <property name="url" value="${jdbc.url}" />
  15. <property name="username" value="${jdbc.username}" />
  16. <property name="password" value="${jdbc.password}" />
  17. </dataSource>
  18. </environment>
  19. </environments>
  20. <mappers>
  21. <mapper resource="User.xml"/>
  22. </mappers>
  23. </configuration>

pojo类

  1. package cn.itheima.pojo;
  2. public class User {
  3. private int id;
  4. private String username;// 用户姓名
  5. //省略……
  6. public int getId() {
  7. return id;
  8. }
  9. public void setId(int id) {
  10. this.id = id;
  11. }
  12. //省略……

基本查询

User.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <!-- namespace:命名空间,做sql隔离 -->
  6. <mapper namespace="test">
  7. <!--
  8. id:sql语句唯一标识
  9. parameterType:指定传入参数类型
  10. resultType:返回结果集类型
  11. #{}占位符:起到占位作用,如果传入的是基本类型(string,long,double,int,boolean,float等),那么#{}中的变量名称可以随意写.
  12. -->
  13. <select id="findUserById" parameterType="java.lang.Integer" resultType="cn.itheima.pojo.User">
  14. select * from user where id=#{id}
  15. </select>
  16. </mapper>

java测试类

  1. public class UserTest {
  2. @Test
  3. public void testFindUserById() throws Exception{
  4. String resource = "SqlMapConfig.xml";
  5. //通过流将核心配置文件读取进来
  6. InputStream inputStream = Resources.getResourceAsStream(resource);
  7. //通过核心配置文件输入流来创建会话工厂
  8. SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
  9. //通过工厂创建会话
  10. SqlSession openSession = factory.openSession();
  11. //第一个参数:所调用的sql语句= namespace+.+sql的ID
  12. User user = openSession.selectOne("test.findUserById", 1);
  13. System.out.println(user);
  14. openSession.close();
  15. }
  16. }
模糊查询

User.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <!-- namespace:命名空间,做sql隔离 -->
  6. <mapper namespace="test">
  7. <!--
  8. 返回结果为集合,可以调用selectList方法,这个方法返回的结果就是一个集合,以映射文件中应该配置成集合泛型的类型
  9. ${}拼接符:字符串原样拼接,如果传入的参数是基本类型(string,long,double,int,boolean,float等),那么${}中的变量名称必须是value
  10. 注意:拼接符有sql注入的风险,所以慎重使用
  11. -->
  12. <select id="findUserByUserName" parameterType="java.lang.String" resultType="cn.itheima.pojo.User">
  13. select * from user where username like '%${value}%'
  14. </select>
  15. </mapper>

java测试类

  1. import cn.itheima.pojo.User;
  2. public class UserTest {
  3. @Test
  4. public void testFindUserbyUserName() throws Exception{
  5. String resource = "SqlMapConfig.xml";
  6. InputStream inputStream = Resources.getResourceAsStream(resource);
  7. SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
  8. SqlSession openSession = factory.openSession();
  9. List<User> list = openSession.selectList("test.findUserByUserName", "王");
  10. System.out.println(list);
  11. }
  12. }

插入数据

User.xml

  1. <!--
  2. #{}:如果传入的是pojo类型,那么#{}中的变量名称必须是pojo中对应的属性.属性.属性.....
  3. 如果要返回数据库自增主键:可以使用select LAST_INSERT_ID()
  4. -->
  5. <insert id="insertUser" parameterType="cn.itheima.pojo.User" >
  6. <!-- 执行 select LAST_INSERT_ID()数据库函数,返回自增的主键
  7. keyProperty:将返回的主键放入传入参数的Id中保存.
  8. order:当前函数相对于insert语句的执行顺序,在insert前执行是before,在insert后执行是AFTER
  9. resultType:id的类型,也就是keyproperties中属性的类型
  10. -->
  11. <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
  12. select LAST_INSERT_ID()
  13. </selectKey>
  14. insert into user (username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})
  15. </insert>

java测试类

  1. @Test
  2. public void testInsertUser() throws Exception{
  3. String resource = "SqlMapConfig.xml";
  4. //通过流将核心配置文件读取进来
  5. InputStream inputStream = Resources.getResourceAsStream(resource);
  6. //通过核心配置文件输入流来创建会话工厂
  7. SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
  8. //通过工厂创建会话
  9. SqlSession openSession = factory.openSession();
  10. User user = new User();
  11. user.setUsername("赵四");
  12. user.setBirthday(new Date());
  13. user.setSex("1");
  14. user.setAddress("北京昌平");
  15. System.out.println("====" + user.getId());
  16. openSession.insert("test.insertUser", user);
  17. //提交事务(mybatis会自动开启事务,但是它不知道何时提交,所以需要手动提交事务)
  18. openSession.commit();
  19. System.out.println("====" + user.getId());
  20. }

删除数据

User.xml

  1. <delete id="delUserById" parameterType="int">
  2. delete from user where id=#{id}
  3. </delete>

java测试类

  1. @Test
  2. public void testDelUserById()throws Exception{
  3. String resource = "SqlMapConfig.xml";
  4. //通过流将核心配置文件读取进来
  5. InputStream inputStream = Resources.getResourceAsStream(resource);
  6. //通过核心配置文件输入流来创建会话工厂
  7. SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
  8. //通过工厂创建会话
  9. SqlSession openSession = factory.openSession();
  10. openSession.delete("test.delUserById", 29);
  11. //提交
  12. openSession.commit();
  13. }

更新数据

User.xml

  1. <update id="updateUserById" parameterType="cn.itheima.pojo.User">
  2. update user set username=#{username} where id=#{id}
  3. </update>

java测试类

  1. @Test
  2. public void testUpdateUserById() throws Exception{
  3. String resource = "SqlMapConfig.xml";
  4. //通过流将核心配置文件读取进来
  5. InputStream inputStream = Resources.getResourceAsStream(resource);
  6. //通过核心配置文件输入流来创建会话工厂
  7. SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
  8. //通过工厂创建会话
  9. SqlSession openSession = factory.openSession();
  10. User user = new User();
  11. user.setId(28);
  12. user.setUsername("王麻子");
  13. openSession.update("test.updateUserById", user);
  14. //提交
  15. openSession.commit();
  16. }

动态代理Dao实现

SqlMapConfig.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <properties resource="db.properties"></properties>
  7. <!-- 和spring整合后 environments配置将废除-->
  8. <environments default="development">
  9. <environment id="development">
  10. <!-- 使用jdbc事务管理-->
  11. <transactionManager type="JDBC" />
  12. <!-- 数据库连接池-->
  13. <dataSource type="POOLED">
  14. <property name="driver" value="${jdbc.driver}" />
  15. <property name="url" value="${jdbc.url}" />
  16. <property name="username" value="${jdbc.username}" />
  17. <property name="password" value="${jdbc.password}" />
  18. </dataSource>
  19. </environment>
  20. </environments>
  21. <mappers>
  22. <!--
  23. 使用class属性引入接口的全路径名称:
  24. 使用规则:
  25. 1. 接口的名称和映射文件名称除扩展名外要完全相同
  26. 2. 接口和映射文件要放在同一个目录下
  27. -->
  28. <mapper class="cn.itheima.mapper.UserMapper"/>
  29. <!-- 使用包扫描的方式批量引入Mapper接口
  30. 使用规则:
  31. 1. 接口的名称和映射文件名称除扩展名外要完全相同
  32. 2. 接口和映射文件要放在同一个目录下
  33. -->
  34. <!-- <package name="cn.itheima.mapper"/> -->
  35. </mappers>
  36. </configuration>

Mapper接口类UserMapper.java

  1. public interface UserMapper {
  2. public User findUserById(Integer id);
  3. //动态代理形势中,如果返回结果集问List,那么mybatis会在生成实现类的使用会自动调用selectList方法
  4. public List<User> findUserByUserName(String userName);
  5. public void insertUser(User user);
  6. }

Mapper接口映射文件UserMapper.xml
路径:与Mapper接口类同目录

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <!--
  6. mapper接口代理实现编写规则:
  7. 1. 映射文件中namespace要等于接口的全路径名称
  8. 2. 映射文件中sql语句id要等于接口的方法名称
  9. 3. 映射文件中传入参数类型要等于接口方法的传入参数类型
  10. 4. 映射文件中返回结果集类型要等于接口方法的返回值类型
  11. -->
  12. <mapper namespace="cn.itheima.mapper.UserMapper">
  13. <!--
  14. id:sql语句唯一标识
  15. parameterType:指定传入参数类型
  16. resultType:返回结果集类型
  17. #{}占位符:起到占位作用,如果传入的是基本类型(string,long,double,int,boolean,float等),那么#{}中的变量名称可以随意写.
  18. -->
  19. <select id="findUserById" parameterType="int" resultType="cn.itheima.pojo.User">
  20. select * from user where id=#{id}
  21. </select>
  22. <!--
  23. 如果返回结果为集合,可以调用selectList方法,这个方法返回的结果就是一个集合,所以映射文件中应该配置成集合泛型的类型
  24. ${}拼接符:字符串原样拼接,如果传入的参数是基本类型(string,long,double,int,boolean,float等),那么${}中的变量名称必须是value
  25. 注意:拼接符有sql注入的风险,所以慎重使用
  26. -->
  27. <select id="findUserByUserName" parameterType="string" resultType="user">
  28. select * from user where username like '%${value}%'
  29. </select>
  30. <!--
  31. #{}:如果传入的是pojo类型,那么#{}中的变量名称必须是pojo中对应的属性.属性.属性.....
  32. 如果要返回数据库自增主键:可以使用select LAST_INSERT_ID()
  33. -->
  34. <insert id="insertUser" parameterType="cn.itheima.pojo.User" >
  35. <!-- 执行 select LAST_INSERT_ID()数据库函数,返回自增的主键
  36. keyProperty:将返回的主键放入传入参数的Id中保存.
  37. order:当前函数相对于insert语句的执行顺序,在insert前执行是before,在insert后执行是AFTER
  38. resultType:id的类型,也就是keyproperties中属性的类型
  39. -->
  40. <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
  41. select LAST_INSERT_ID()
  42. </selectKey>
  43. insert into user (username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})
  44. </insert>
  45. </mapper>

java测试类

  1. public class UserMapperTest {
  2. private SqlSessionFactory factory;
  3. //作用:在测试方法前执行这个方法
  4. @Before
  5. public void setUp() throws Exception{
  6. String resource = "SqlMapConfig.xml";
  7. //通过流将核心配置文件读取进来
  8. InputStream inputStream = Resources.getResourceAsStream(resource);
  9. //通过核心配置文件输入流来创建会话工厂
  10. factory = new SqlSessionFactoryBuilder().build(inputStream);
  11. }
  12. @Test
  13. public void testFindUserById() throws Exception{
  14. SqlSession openSession = factory.openSession();
  15. //通过getMapper方法来实例化接口
  16. UserMapper mapper = openSession.getMapper(UserMapper.class);
  17. User user = mapper.findUserById(1);
  18. System.out.println(user);
  19. }
  20. @Test
  21. public void testFindUserByUserName() throws Exception{
  22. SqlSession openSession = factory.openSession();
  23. //通过getMapper方法来实例化接口
  24. UserMapper mapper = openSession.getMapper(UserMapper.class);
  25. List<User> list = mapper.findUserByUserName("王");
  26. }
  27. @Test
  28. public void testInsertUser() throws Exception{
  29. SqlSession openSession = factory.openSession();
  30. //通过getMapper方法来实例化接口
  31. UserMapper mapper = openSession.getMapper(UserMapper.class);
  32. User user = new User();
  33. user.setUsername("老王");
  34. user.setSex("1");
  35. user.setBirthday(new Date());
  36. user.setAddress("北京昌平");
  37. mapper.insertUser(user);
  38. openSession.commit();
  39. }
  40. }

SqlMapConfig核心配置文件

SqlMapConfig.xml
目录:项目\config\

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <properties resource="db.properties"></properties>
  7. <typeAliases>
  8. <!-- 定义单个pojo类别名
  9. type:类的全路劲名称
  10. alias:别名
  11. -->
  12. <!-- <typeAlias type="cn.itheima.pojo.User" alias="user"/> -->
  13. <!-- 使用包扫描的方式批量定义别名
  14. 定以后别名等于类名,不区分大小写,但是建议按照java命名规则来,首字母小写,以后每个单词的首字母大写
  15. -->
  16. <package name="cn.itheima.pojo"/>
  17. </typeAliases>
  18. <!-- 和spring整合后 environments配置将废除-->
  19. <environments default="development">
  20. <environment id="development">
  21. <!-- 使用jdbc事务管理-->
  22. <transactionManager type="JDBC" />
  23. <!-- 数据库连接池-->
  24. <dataSource type="POOLED">
  25. <property name="driver" value="${jdbc.driver}" />
  26. <property name="url" value="${jdbc.url}" />
  27. <property name="username" value="${jdbc.username}" />
  28. <property name="password" value="${jdbc.password}" />
  29. </dataSource>
  30. </environment>
  31. </environments>
  32. <mappers>
  33. <mapper resource="User.xml"/>
  34. <!--
  35. 使用class属性引入接口的全路径名称:
  36. 使用规则:
  37. 1. 接口的名称和映射文件名称除扩展名外要完全相同
  38. 2. 接口和映射文件要放在同一个目录下
  39. -->
  40. <!-- <mapper class="cn.itheima.mapper.UserMapper"/> -->
  41. <!-- 使用包扫描的方式批量引入Mapper接口
  42. 使用规则:
  43. 1. 接口的名称和映射文件名称除扩展名外要完全相同
  44. 2. 接口和映射文件要放在同一个目录下
  45. -->
  46. <package name="cn.itheima.mapper"/>
  47. </mappers>
  48. </configuration>

动态sql语句

if where

UserMapper.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="cn.itheima.mapper.UserMapper">
  6. <!-- 封装sql条件,封装后可以重用.
  7. id:是这个sql条件的唯一标识 -->
  8. <sql id="user_Where">
  9. <!-- where标签作用:
  10. 会自动向sql语句中添加where关键字
  11. 会去掉第一个条件的and关键字
  12. -->
  13. <where>
  14. <if test="username != null and username != ''">
  15. and username like '%${username}%'
  16. </if>
  17. <if test="sex != null and sex != ''">
  18. and sex=#{sex}
  19. </if>
  20. </where>
  21. </sql>
  22. <select id="findUserByUserNameAndSex" parameterType="cn.itheima.pojo.User" resultType="cn.itheima.pojo.User">
  23. select * from user
  24. <!-- 调用sql条件 -->
  25. <include refid="user_Where"></include>
  26. </select>
  27. </mapper>

UserMapper.java

  1. public interface UserMapper {
  2. public List<User> findUserByUserNameAndSex(User user);
  3. }

java测试类

  1. public class UserMapperTest {
  2. private SqlSessionFactory factory;
  3. //作用:在测试方法前执行这个方法
  4. @Before
  5. public void setUp() throws Exception{
  6. String resource = "SqlMapConfig.xml";
  7. //通过流将核心配置文件读取进来
  8. InputStream inputStream = Resources.getResourceAsStream(resource);
  9. //通过核心配置文件输入流来创建会话工厂
  10. factory = new SqlSessionFactoryBuilder().build(inputStream);
  11. }
  12. @Test
  13. public void testFindUserbyUserNameAndSex() throws Exception{
  14. SqlSession openSession = factory.openSession();
  15. //通过getMapper方法来实例化接口
  16. UserMapper mapper = openSession.getMapper(UserMapper.class);
  17. User user = new User();
  18. user.setUsername("王");
  19. user.setSex("1");
  20. List<User> list = mapper.findUserByUserNameAndSex(user);
  21. System.out.println(list);
  22. }
  23. }

foreach

UserMapper.xml

  1. <select id="findUserByIds" parameterType="cn.itheima.pojo.QueryVo" resultType="cn.itheima.pojo.User">
  2. <!--select * from user where id in (1,16,28,22)-->
  3. select * from user
  4. <where>
  5. <if test="ids != null">
  6. <!--
  7. foreach:循环传入的集合参数
  8. collection:传入的集合的变量名称
  9. item:每次循环将循环出的数据放入这个变量中
  10. open:循环开始拼接的字符串
  11. close:循环结束拼接的字符串
  12. separator:循环中拼接的分隔符
  13. -->
  14. <foreach collection="ids" item="id" open="id in (" close=")" separator=",">
  15. #{id}
  16. </foreach>
  17. </if>
  18. </where>
  19. </select>

UserMapper.java

  1. public interface UserMapper {
  2. public List<User> findUserByIds(QueryVo vo);
  3. }

VO(View Object)类

  1. public class QueryVo {
  2. private User user;
  3. private List<Integer> ids;
  4. //省略……
  5. public List<Integer> getIds() {
  6. return ids;
  7. }
  8. public void setIds(List<Integer> ids) {
  9. this.ids = ids;
  10. }

java测试类

  1. public class UserMapperTest {
  2. @Test
  3. public void testFindUserbyIds() throws Exception{
  4. SqlSession openSession = factory.openSession();
  5. //通过getMapper方法来实例化接口
  6. UserMapper mapper = openSession.getMapper(UserMapper.class);
  7. QueryVo vo = new QueryVo();
  8. List<Integer> ids = new ArrayList<Integer>();
  9. ids.add(1);
  10. ids.add(16);
  11. ids.add(28);
  12. ids.add(22);
  13. vo.setIds(ids);
  14. List<User> list = mapper.findUserByIds(vo);
  15. System.out.println(list);
  16. }
  17. }

关联查询

一对一关系

自定义POJO对象类:用于自动映射

  1. //继承Orders类加入User类属性
  2. public class CustomOrders extends Orders{
  3. private int uid;
  4. private String username;// 用户姓名
  5. //省略……
  6. public int getUid() {
  7. return uid;
  8. }
  9. public void setUid(int uid) {
  10. this.uid = uid;
  11. }
  12. public String getUsername() {
  13. return username;
  14. }
  15. //省略……
  16. }

POJO对象类

  1. public class Orders {
  2. private Integer id;
  3. private Integer userId;
  4. //省略……
  5. private User user;
  6. public Integer getId() {
  7. return id;
  8. }
  9. public void setId(Integer id) {
  10. this.id = id;
  11. }
  12. public Integer getUserId() {
  13. return userId;
  14. }
  15. public void setUserId(Integer userId) {
  16. this.userId = userId;
  17. }
  18. //省略……
  19. public User getUser() {
  20. return user;
  21. }
  22. public void setUser(User user) {
  23. this.user = user;
  24. }
  25. }
  1. public class User {
  2. private int id;
  3. //省略……
  4. public int getId() {
  5. return id;
  6. }
  7. public void setId(int id) {
  8. this.id = id;
  9. }

UserMapper.xml

  1. <!-- 一对一:自动映射 -->
  2. <select id="findOrdersAndUser1" resultType="cn.itheima.pojo.CustomOrders">
  3. select a.*, b.id uid, username, birthday, sex, address
  4. from orders a, user b
  5. where a.user_id = b.id
  6. </select>
  7. <!-- 一对一:手动映射 -->
  8. <!--
  9. id:resultMap的唯一标识
  10. type:将查询出的数据放入这个指定的对象中
  11. 注意:手动映射需要指定数据库中表的字段名与java中pojo类的属性名称的对应关系
  12. -->
  13. <resultMap type="cn.itheima.pojo.Orders" id="orderAndUserResultMap">
  14. <!-- id标签指定主键字段对应关系
  15. column:列,数据库中的字段名称
  16. property:属性,java中pojo中的属性名称
  17. -->
  18. <id column="id" property="id"/>
  19. <!-- result:标签指定非主键字段的对应关系 -->
  20. <result column="user_id" property="userId"/>
  21. <result column="number" property="number"/>
  22. <result column="createtime" property="createtime"/>
  23. <result column="note" property="note"/>
  24. <!-- 这个标签指定单个对象的对应关系
  25. property:指定将数据放入Orders中的user属性中
  26. javaType:user属性的类型
  27. -->
  28. <association property="user" javaType="cn.itheima.pojo.User">
  29. <id column="uid" property="id"/>
  30. <result column="username" property="username"/>
  31. <result column="birthday" property="birthday"/>
  32. <result column="sex" property="sex"/>
  33. <result column="address" property="address"/>
  34. </association>
  35. </resultMap>
  36. <select id="findOrdersAndUser2" resultMap="orderAndUserResultMap">
  37. select a.*, b.id uid, username, birthday, sex, address
  38. from orders a, user b
  39. where a.user_id = b.id
  40. </select>

UserMapper.java接口类

  1. public interface UserMapper {
  2. public List<CustomOrders> findOrdersAndUser1() ;
  3. public List<Orders> findOrdersAndUser2();
  4. }

java测试类

  1. public class UserMapperTest {
  2. private SqlSessionFactory factory;
  3. //作用:在测试方法前执行这个方法
  4. @Before
  5. public void setUp() throws Exception{
  6. String resource = "SqlMapConfig.xml";
  7. //通过流将核心配置文件读取进来
  8. InputStream inputStream = Resources.getResourceAsStream(resource);
  9. //通过核心配置文件输入流来创建会话工厂
  10. factory = new SqlSessionFactoryBuilder().build(inputStream);
  11. }
  12. @Test
  13. public void testFindOrdersAndUser() throws Exception{
  14. SqlSession openSession = factory.openSession();
  15. //通过getMapper方法来实例化接口
  16. UserMapper mapper = openSession.getMapper(UserMapper.class);
  17. List<CustomOrders> list = mapper.findOrdersAndUser1();
  18. System.out.println(list);
  19. }
  20. @Test
  21. public void testFindOrdersAnduUser2() throws Exception{
  22. SqlSession openSession = factory.openSession();
  23. //通过getMapper方法来实例化接口
  24. UserMapper mapper = openSession.getMapper(UserMapper.class);
  25. List<Orders> list = mapper.findOrdersAndUser2();
  26. System.out.println(list);
  27. }
  28. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注