mabits

一、传入参数的传递

parameterType指定参数类型

基本类型参数(int、string.......)

pojo类型:user对象

map类型

包装类型

1、map类型的传递

需求:查询用户性别为男,姓张的用户

 
  1. namespace="com.itcast.dao.UserMapper" >
  2. id="findUserWithMap" parameterType="map" resultType="user" >
  3. select * from user where sex=#{sex} and username like "%"#{username}"%"
 
  1. @Test
  2. public void fun1() throws IOException{
  3. InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
  4. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  5. SqlSession sqlSession = sqlSessionFactory.openSession();
  6. UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
  7. Map<String, Object> map = new HashMap<>();
  8. map.put("sex", "男");
  9. map.put("username", "张");
  10. List<User> list = userMapper.findUserWithMap(map);
  11. System.out.println(list);
  12. }

2、包装类型的传递

需求:查询用户性别为男,姓张的用户

 
  1. id="findUserWithQueryVo" parameterType="queryvo" resultType="user" >
  2. select * from user where sex=#{user.sex} and username like "%"#{user.username}"%"
 
  1. @Test
  2. public void fun2() throws IOException{
  3. InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
  4. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  5. SqlSession sqlSession = sqlSessionFactory.openSession();
  6. UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
  7. User user = new User();
  8. user.setSex("男");
  9. user.setUsername("张");
  10. QueryVo queryVo = new QueryVo();
  11. queryVo.setUser(user);
  12. List<User> list = userMapper.findUserWithQueryVo(queryVo);
  13. System.out.println(list);
  14. }

 

二、返回参数的类型

基本类型

pojo类型

返回集合类型

 

1、返回基本类型

需求:查询用户性别为男,姓张的用户总计数

 
  1. id="findUserWithQueryVoCount" parameterType="queryvo" resultType="int" >
  2. select count(2) from user where sex=#{user.sex} and username like "%"#{user.username}"%"
 
  1. @Test
  2. public void fun3() throws IOException{
  3. InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
  4. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  5. SqlSession sqlSession = sqlSessionFactory.openSession();
  6. UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
  7. User user = new User();
  8. user.setSex("男");
  9. user.setUsername("张");
  10. QueryVo queryVo = new QueryVo();
  11. queryVo.setUser(user);
  12. int count = userMapper.findUserWithQueryVoCount(queryVo);
  13. System.out.println(count);
  14. }

2、resultType特性

特性:查询数据库列名必须和映射的javabean属性名称一一对应,且名称相同,否则不能映射成功

验证:

 
  1. id="findUserWithResultType" resultType="user" >
  2. select id _id,username _username,birthday _birthday,sex _sex,address _address from user
 
  1. @Test
  2. public void fun4() throws IOException{
  3. InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
  4. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  5. SqlSession sqlSession = sqlSessionFactory.openSession();
  6. UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
  7. List<User> list= userMapper.findUserWithResultType();
  8. System.out.println(list);
  9. }

 

返回结果错误:[null, null, null, null, null, null, null, null, null]

 

解决映射不成功的方案:

    *    定义别名

    *    resultMap

 

3、resultMap映射特点

解决resultType 数据库列名与javabean属性名不相同不能映射的问题

需要先定义一个resultMap的映射关系

                    主键映射 :使用id

        column:查询数据库列名

        property:javabean属性名

 
  1. type="user" id="userMap">
  2. column="_id" property="id" />
  3.  
  4. column="_username" property="username" />
  5. column="_birthday" property="birthday" />
  6. column="_sex" property="sex" />
  7. column="_address" property="address" />
  8.  
  9. id="findUserWithResultMap" resultMap="userMap" >
  10. select id _id,username _username,birthday _birthday,sex _sex,address _address from user

java代码和上面一样

 

 

三、多表关联的查询:

1、多的一方    一对一查询

   可以用resultType也可以用resultMap查询

①  用resultType来映射

需求:通过订单查询用户和订单所有数据,一个订单只对应一个用户,所以是一对一查询

新定义一个订单实体类,再用一个OrdersCustom类继承Orders类中所有属性,再添加User类中的所有属性,注意把User类中的主键名字改成uid,因为不能和Orders的主键id名字重复啊,查询后的结果就封装在OrdersCustom这个类的属性中

 
  1. namespace="com.itcast.dao.OrdersMapper" >
  2. id="findOrdersWithUserByResultType" resultType="ordersCustom" >
  3. select o.id,o.user_id userId,o.number,o.createtime createTime,o.note,
  4. u.id uid,u.username,u.birthday,u.sex,u.address
  5. from user u,orders o where u.id=o.user_id
 
  1. @Test
  2. public void fun1() throws IOException{
  3. InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
  4. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  5. SqlSession sqlSession = sqlSessionFactory.openSession();
  6. OrdersMapper ordersMapper = sqlSession.getMapper(OrdersMapper.class);
  7. List<OrdersCustom> list = ordersMapper.findOrdersWithUserByResultType();
  8. System.out.println(list);
  9. }

 

②用resultMap来映射

                       配置一对一关系映射:association 

property:指定映射orders中那个属性

javaType:指定关系映射对象类型

 
  1. namespace="com.itcast.dao.OrdersMapper" >
  2. type="orders" id="ordersMap">
  3. column="id" property="id" />
  4.  
  5. column="user_id" property="userId" />
  6. column="number" property="number" />
  7. column="createtime" property="createTime" />
  8. column="note" property="note" />
  9.  
  10. property="user" javaType="user">
  11. column="uid" property="id" />
  12.  
  13. column="username" property="username" />
  14. column="birthday" property="birthday" />
  15. column="sex" property="sex" />
  16. column="address" property="address" />
  17.  
  18. id="findOrdersWithUserByResultMap" resultMap="ordersMap" >
  19. SELECT orders.*,user.id uid,user.username,user.birthday,user.sex,user.address
  20. FROM orders,user WHERE orders.user_id = user.id
 
  1. @Test
  2. public void fun2() throws IOException{
  3. InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
  4. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  5. SqlSession sqlSession = sqlSessionFactory.openSession();
  6. OrdersMapper ordersMapper = sqlSession.getMapper(OrdersMapper.class);
  7. List<Orders> list = ordersMapper.findOrdersWithUserByResultMap();
  8. System.out.println(list);
  9. }

 

 

2、一的一方    一对多查询

一个用户对应了多个订单,如果通过用户查询用户数据和对应的订单数据,那就是一对多查询

一对多查询只能用resultMap来映射

 
  1. namespace="com.itcast.dao.UserMapper" >
  2. type="user" id="userMap">
  3. column="uid" property="id" />
  4. column="username" property="username"/>
  5. column="birthday" property="birthday"/>
  6. column="sex" property="sex"/>
  7. column="address" property="address"/>
  8. property="oList" ofType="orders" >
  9. column="id" property="id"/>
  10. column="user_id" property="userId"/>
  11. column="number" property="number"/>
  12. column="createtime" property="createTime"/>
  13. column="note" property="note"/>
  14.  
  15.  
  16. id="findUserWithOrdersByMap" resultMap="userMap" >
  17. select user.id uid,user.username,user.birthday,user.sex,user.address,
  18. orders.id,orders.user_id userId,orders.number,orders.createtime createTime,orders.note
  19. from user,orders where orders.user_id=user.id
 
  1. @Test
  2. public void fun3() throws IOException{
  3. InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
  4. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  5. SqlSession sqlSession = sqlSessionFactory.openSession();
  6. UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
  7. List<User> list = userMapper.findUserWithOrdersByMap();
  8. System.out.println(list);
  9. }

 

 

四、动态sql查询

1、if标签和where标签连用

       where标签自动生成where语句,并且where标签可以自动去掉sql语句的第一个多余的“and”

       if标签::当if标签传入的是pojo类型,或者包装类型,test里面就直接写传入类型里面的属性名,但是当传入的parameterType是基本数据类型,例如string,如要判断string类型的内容,则需要在test里面判断:_parameter

①不用where标签的做法:

 
  1. id="findUserWithQueryVo" parameterType="queryvo" resultType="user" >
  2. select * from user where 1=1
  3. test="user.sex!=null and user.sex!=''">
  4. and sex=#{user.sex}
  5. test="user.username!=null and user.username!=''">
  6. and username like "%"#{user.username}"%"
 
  1. @Test
  2. public void fun2() throws IOException{
  3. InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
  4. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  5. SqlSession sqlSession = sqlSessionFactory.openSession();
  6. UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
  7. User user = new User();
  8. user.setSex(null);
  9. user.setUsername("张");
  10. QueryVo queryVo = new QueryVo();
  11. queryVo.setUser(user);
  12. List<User> list = userMapper.findUserWithQueryVo(queryVo);
  13. System.out.println(list);
  14. }

 

②用where标签的做法:

 
  1. id="findUserWithQueryVo" parameterType="queryvo" resultType="user" >
  2. select * from user
  3. test="user.sex!=null and user.sex!=''">
  4. and sex=#{user.sex}
  5. test="user.username!=null and user.username!=''">
  6. and username like "%"#{user.username}"%"

java程序和上面一样

 

 

2、动态更新语句:

标签类似于where标签

 

 

 

 

3、sql片段

       把相同的sql片段抽取出去

标签定义片段;;来引入sql片段

id和refid相对应

 
  1. namespace="com.itcast.dao.UserMapper" >
  2.  
  3. id="where_if_vo">
  4. test="user.sex!=null and user.sex!=''">
  5. and sex=#{user.sex}
  6. test="user.username!=null and user.username!=''">
  7. and username like "%"#{user.username}"%"
  8.  
  9.  
  10. id="findUserWithQueryVo" parameterType="queryvo" resultType="user" >
  11. select * from user
  12. refid="where_if_vo">
  13.  
  14. id="findUserWithQueryVoCount" parameterType="queryvo" resultType="int" >
  15. select count(2) from user
  16. refid="where_if_vo">

 

4、foreach动态遍历集合参数

①第一种方法:

    select * from user where (id=22 or id=23 or id=33)

   

 
  1. id="findUserWithOr" parameterType="queryvo" resultType="user" >
  2. select * from user
  3. collection="ids" item="id" open="(" separator="or" close=")" >
  4. id=#{id}
 
  1. @Test
  2. public void fun4() throws IOException{
  3. InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
  4. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  5. SqlSession sqlSession = sqlSessionFactory.openSession();
  6. UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
  7. QueryVo queryVo = new QueryVo();
  8. List<Integer> ids = new ArrayList<>();
  9. ids.add(22);
  10. ids.add(24);
  11. ids.add(26);
  12. queryVo.setIds(ids);
  13. List<User> list = userMapper.findUserWithOr(queryVo);
  14. System.out.println(list);
  15. }

 

②另一种方法:

select *  from user where id in (22,23,33)

 
  1. id="findUserWithIn" parameterType="queryvo" resultType="user" >
  2. select * from user
  3. collection="ids" item="id" open="id IN(" separator="," close=")" >
  4. #{id}

java代码只要改个方法名即可

 

arraylist、list=======》list

数组====》array

 

 

 

 

五、mybatis和spring整合

spring配置文件:

 
  1. xml version="1.0" encoding="UTF-8"?>
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:aop="http://www.springframework.org/schema/aop"
  4. xmlns="http://www.springframework.org/schema/beans"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/aop
  8. http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
  9. http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context-4.2.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
  15.  
  16.  
  17. location="classpath:jdbc.properties" />
  18. id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" >
  19. name="driverClassName" value="${jdbc.driver}">
  20. name="url" value="${jdbc.url}">
  21. name="username" value="${jdbc.username}">
  22. name="password" value="${jdbc.password}">
  23.  
  24. id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
  25. name="dataSource" ref="dataSource" >
  26. name="typeAliasesPackage" value="com.itcast.domain" >
  27. name="configLocation" value="classpath:sqlMapConfig.xml" >
  28.  
  29. class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
  30. name="basePackage" value="com.itcast.dao" >

另一种扫描接口的方法:

或者直接在sqlMapConfig.xml里面配置

 

1、传统模式整合:

applicationContext.xml配置

 
  1. xml version="1.0" encoding="UTF-8"?>
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:aop="http://www.springframework.org/schema/aop"
  4. xmlns="http://www.springframework.org/schema/beans"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/aop
  8. http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
  9. http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context-4.2.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
  15.  
  16.  
  17. location="classpath:jdbc.properties" />
  18. id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" >
  19. name="driverClassName" value="${jdbc.driver}">
  20. name="url" value="${jdbc.url}">
  21. name="username" value="${jdbc.username}">
  22. name="password" value="${jdbc.password}">
  23.  
  24. id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
  25. name="dataSource" ref="dataSource" >
  26. name="typeAliasesPackage" value="com.itcast.domain" >
  27. name="configLocation" value="classpath:sqlMapConfig.xml" >
  28.  
  29.  
  30. class="com.itcast.dao.impl.UserMapperImpl" >
  31. name="sqlSessionFactory" ref="sqlSessionFactory" >
  32.  

 

sqlMapConfig.xml配置:

 
  1. resource="com/itcast/dao/UserMapper.xml" />

使用传统模式,有实现类

 
  1. public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper{
  2. @Override
  3. public List<User> findUserWithQueryVo(QueryVo queryVo) {
  4. List<User> list = this.getSqlSession().selectList("com.itcast.dao.UserMapper.findUserWithQueryVo", queryVo);
  5. return list;
  6. }
  7. }

测试类:

 
  1. @Test
  2. public void fun2() throws IOException{
  3. ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
  4. UserMapperImpl mapperImpl = context.getBean(UserMapperImpl.class);
  5. User user = new User();
  6. user.setSex("男");
  7. user.setUsername("张");
  8. QueryVo queryVo = new QueryVo();
  9. queryVo.setUser(user);
  10. List<User> list = mapperImpl.findUserWithQueryVo(queryVo);
  11. System.out.println(list);
  12. }

 

2、接口代理模式整合:

applicationContext.xml加上接口扫描即可

 

测试:

UserMapper bean = context.getBean(UserMapper.class);

 

 

 

 

 

 

 

 

你可能感兴趣的:(javaweb技术)