mabits

一、传入参数的传递

parameterType指定参数类型

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

pojo类型:user对象

map类型

包装类型

1、map类型的传递

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

 
  1. <mapper namespace="com.itcast.dao.UserMapper" >
  2. <!-- 需求:查询用户性别为男,姓张的用户 -->
  3. <select id="findUserWithMap" parameterType="map" resultType="user" >
  4. select * from user where sex=#{sex} and username like "%"#{username}"%"
  5. </select>
  6. </mapper>
 
  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. <!-- 需求:查询用户性别为男,姓张的用户 -->
  2. <select id="findUserWithQueryVo" parameterType="queryvo" resultType="user" >
  3. select * from user where sex=#{user.sex} and username like "%"#{user.username}"%"
  4. </select>
 
  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. <!-- 查询用户性别为男,姓张的用户总记录数 -->
  2. <select id="findUserWithQueryVoCount" parameterType="queryvo" resultType="int" >
  3. select count(2) from user where sex=#{user.sex} and username like "%"#{user.username}"%"
  4. </select>
 
  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. <!-- 验证resultType的映射特性-->
  2. <select id="findUserWithResultType" resultType="user" >
  3. select id _id,username _username,birthday _birthday,sex _sex,address _address from user
  4. </select>
 
  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. <!-- 定义resultMap -->
  2. <resultMap type="user" id="userMap">
  3. <!-- 主键映射 -->
  4. <id column="_id" property="id" />
  5.  
  6. <!-- 普通属性映射 -->
  7. <result column="_username" property="username" />
  8. <result column="_birthday" property="birthday" />
  9. <result column="_sex" property="sex" />
  10. <result column="_address" property="address" />
  11. </resultMap>
  12.  
  13. <!-- resultMap解决数据库列名与javabean属性名不相同不能映射的状况-->
  14. <select id="findUserWithResultMap" resultMap="userMap" >
  15. select id _id,username _username,birthday _birthday,sex _sex,address _address from user
  16. </select>

java代码和上面一样

 

 

三、多表关联的查询:

1、多的一方    一对一查询

   可以用resultType也可以用resultMap查询

①  用resultType来映射

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

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

 
  1. <mapper namespace="com.itcast.dao.OrdersMapper" >
  2. <select 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
  6. </select>
  7. </mapper>
 
  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. <mapper namespace="com.itcast.dao.OrdersMapper" >
  2. <!--
  3. 定义关系映射:resultMap
  4. type:指定映射类型
  5. id:resultMap映射关系唯一标识
  6. -->
  7. <resultMap type="orders" id="ordersMap">
  8. <!--
  9. 主键映射 :使用id
  10. column:查询数据库列名
  11. property:javabean属性名
  12. -->
  13. <id column="id" property="id" />
  14.  
  15. <!-- 普通属性映射 -->
  16. <result column="user_id" property="userId" />
  17. <result column="number" property="number" />
  18. <result column="createtime" property="createTime" />
  19. <result column="note" property="note" />
  20.  
  21. <!-- 表关联
  22. 配置一对一关系映射:association
  23. property:指定映射orders中那个属性
  24. javaType:指定关系映射对象类型
  25. -->
  26. <association property="user" javaType="user">
  27. <id column="uid" property="id" />
  28.  
  29. <result column="username" property="username" />
  30. <result column="birthday" property="birthday" />
  31. <result column="sex" property="sex" />
  32. <result column="address" property="address" />
  33. </association>
  34. </resultMap>
  35.  
  36. <select id="findOrdersWithUserByResultMap" resultMap="ordersMap" >
  37. SELECT orders.*,user.id uid,user.username,user.birthday,user.sex,user.address
  38. FROM orders,user WHERE orders.user_id = user.id
  39. </select>
  40. </mapper>
 
  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. <mapper namespace="com.itcast.dao.UserMapper" >
  2. <resultMap type="user" id="userMap">
  3. <id column="uid" property="id" />
  4. <result column="username" property="username"/>
  5. <result column="birthday" property="birthday"/>
  6. <result column="sex" property="sex"/>
  7. <result column="address" property="address"/>
  8. <!-- ofType:制定映射的javabean类型,property:表示映射到user中的那个属性 -->
  9. <collection property="oList" ofType="orders" >
  10. <!--
  11. 主键映射 :使用id
  12. column:查询数据库列名
  13. property:javabean属性名
  14. -->
  15. <id column="id" property="id"/>
  16. <!-- 普通属性映射 -->
  17. <result column="user_id" property="userId"/>
  18. <result column="number" property="number"/>
  19. <result column="createtime" property="createTime"/>
  20. <result column="note" property="note"/>
  21. </collection>
  22. </resultMap>
  23.  
  24.  
  25. <!-- 查询用户订单,一对多 -->
  26. <select id="findUserWithOrdersByMap" resultMap="userMap" >
  27. select user.id uid,user.username,user.birthday,user.sex,user.address,
  28. orders.id,orders.user_id userId,orders.number,orders.createtime createTime,orders.note
  29. from user,orders where orders.user_id=user.id
  30. </select>
  31. </mapper>
 
  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. <!-- 需求:查询用户性别为男,姓张的用户 -->
  2. <select id="findUserWithQueryVo" parameterType="queryvo" resultType="user" >
  3. select * from user where 1=1
  4. <if test="user.sex!=null and user.sex!=''">
  5. and sex=#{user.sex}
  6. </if>
  7. <if test="user.username!=null and user.username!=''">
  8. and username like "%"#{user.username}"%"
  9. </if>
  10. </select>
 
  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. <!-- 需求:查询用户性别为男,姓张的用户 -->
  2. <select id="findUserWithQueryVo" parameterType="queryvo" resultType="user" >
  3. select * from user
  4. <!-- where标签自动生成where语句,并且去掉第一个多余的and -->
  5. <where>
  6. <if test="user.sex!=null and user.sex!=''">
  7. and sex=#{user.sex}
  8. </if>
  9. <if test="user.username!=null and user.username!=''">
  10. and username like "%"#{user.username}"%"
  11. </if>
  12. </where>
  13. </select>

java程序和上面一样

 

 

2、动态更新语句:

<set>标签类似于where标签

 

 

 

 

3、sql片段

       把相同的sql片段抽取出去

<sql id="" ></sql>标签定义片段;;<include refid="" ></include>来引入sql片段

id和refid相对应

 
  1. <mapper namespace="com.itcast.dao.UserMapper" >
  2.  
  3. <!-- 定义一个sql片段,抽取公共的sql语句 -->
  4. <sql id="where_if_vo">
  5. <where>
  6. <if test="user.sex!=null and user.sex!=''">
  7. and sex=#{user.sex}
  8. </if>
  9. <if test="user.username!=null and user.username!=''">
  10. and username like "%"#{user.username}"%"
  11. </if>
  12. </where>
  13. </sql>
  14.  
  15.  
  16. <!-- 需求:查询用户性别为男,姓张的用户 -->
  17. <select id="findUserWithQueryVo" parameterType="queryvo" resultType="user" >
  18. select * from user
  19. <!-- where标签自动生成where语句,并且去掉第一个多余的and -->
  20. <include refid="where_if_vo"></include>
  21. </select>
  22.  
  23. <!-- 查询用户性别为男,姓张的用户总记录数 -->
  24. <select id="findUserWithQueryVoCount" parameterType="queryvo" resultType="int" >
  25. select count(2) from user
  26. <include refid="where_if_vo"></include>
  27. </select>
  28. </mapper>

 

4、foreach动态遍历集合参数

①第一种方法:

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

   

 
  1. <!-- 接收集合参数 -->
  2. <select id="findUserWithOr" parameterType="queryvo" resultType="user" >
  3. select * from user
  4. <where>
  5. <foreach collection="ids" item="id" open="(" separator="or" close=")" >
  6. id=#{id}
  7. </foreach>
  8. </where>
  9. </select>
 
  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. <!-- 接收集合参数 -->
  2. <select id="findUserWithIn" parameterType="queryvo" resultType="user" >
  3. select * from user
  4. <where>
  5. <foreach collection="ids" item="id" open="id IN(" separator="," close=")" >
  6. #{id}
  7. </foreach>
  8. </where>
  9. </select>

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

 

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

数组====》array

 

 

 

 

五、mybatis和spring整合

spring配置文件:

 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans 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. <context:property-placeholder location="classpath:jdbc.properties" />
  18. <!-- 整合持久层框架 -->
  19. <!-- 数据源 -->
  20. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" >
  21. <property name="driverClassName" value="${jdbc.driver}"></property>
  22. <property name="url" value="${jdbc.url}"></property>
  23. <property name="username" value="${jdbc.username}"></property>
  24. <property name="password" value="${jdbc.password}"></property>
  25. </bean>
  26.  
  27. <!-- 创建工厂,生成sqlSession -->
  28. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
  29. <property name="dataSource" ref="dataSource" ></property>
  30. <!-- 扫描别名 -->
  31. <property name="typeAliasesPackage" value="com.itcast.domain" ></property>
  32. <!-- 加载sqlMapConfig.xml文件 -->
  33. <property name="configLocation" value="classpath:sqlMapConfig.xml" ></property>
  34. </bean>
  35.  
  36. <!-- 扫描接口,开启接口代理方式开发 -->
  37. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
  38. <property name="basePackage" value="com.itcast.dao" ></property>
  39. </bean>
  40. </beans>

另一种扫描接口的方法:

<bean class="org.mybatis.spring.mapper.MapperFactoryBean">

<property name="mapperInterface" value="cn.itcast.mybatis.mapper.UserMapper"/>

<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>

</bean>

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

 

1、传统模式整合:

applicationContext.xml配置

 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans 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. <context:property-placeholder location="classpath:jdbc.properties" />
  18. <!-- 整合持久层框架 -->
  19. <!-- 数据源 -->
  20. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" >
  21. <property name="driverClassName" value="${jdbc.driver}"></property>
  22. <property name="url" value="${jdbc.url}"></property>
  23. <property name="username" value="${jdbc.username}"></property>
  24. <property name="password" value="${jdbc.password}"></property>
  25. </bean>
  26.  
  27. <!-- 创建工厂,生成sqlSession -->
  28. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
  29. <property name="dataSource" ref="dataSource" ></property>
  30. <!-- 扫描别名 -->
  31. <property name="typeAliasesPackage" value="com.itcast.domain" ></property>
  32. <!-- 加载sqlMapConfig.xml文件 -->
  33. <property name="configLocation" value="classpath:sqlMapConfig.xml" ></property>
  34. </bean>
  35.  
  36.  
  37. <!-- 定义dao对象,注入工厂对象 -->
  38. <bean class="com.itcast.dao.impl.UserMapperImpl" >
  39. <property name="sqlSessionFactory" ref="sqlSessionFactory" ></property>
  40. </bean>
  41.  
  42. </beans>

 

sqlMapConfig.xml配置:

 
  1. <configuration>
  2. <mappers>
  3. <!-- <package name="com.itcast.dao"/> -->
  4. <mapper resource="com/itcast/dao/UserMapper.xml" />
  5. </mappers>
  6. </configuration>

使用传统模式,有实现类

 
  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);

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值