Mybatis 和Spring整合之mapper代理开发

大鹏一日同风起,扶摇直上九万里。这篇文章主要讲述Mybatis 和Spring整合之mapper代理开发相关的知识,希望能为你提供帮助。
F:\\1ziliao\\mybatis\\代码

Mybatis 和Spring整合之mapper代理开发

文章图片

 
1.1  SqlMapConfig.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>

< !-- 通过setting配置mybatis的运行参数
注意,设置运行参数会影响 mybatis的运行,一定要注意!
-->
< settings>
< !-- 延迟加载的总开关 -->
< setting name="lazyLoadingEnabled" value="https://www.songbingjia.com/android/true"/>
< !-- 设置为false实现按需求加载-->
< setting name="aggressiveLazyLoading" value="https://www.songbingjia.com/android/false"/>
< !-- 开启二级缓存 -->
< setting name="cacheEnabled" value="https://www.songbingjia.com/android/true"/>
< /settings>
< !-- 定义别名 -->

< typeAliases>
< !-- 单个别名定义
type:类路径
alias:别名
-->
< !-- < typeAlias type="cn.itcast.mybatis.po.User" alias="user"/> -->
< !-- 批量配置
指定pojo所在包路径,自动扫描包下的pojo定义别名,别名为类名(首字母小写或大写都可以)
-->
< package name="cn.itcast.mybatis.po"/>
< !-- 如果扫描多个包中的pojo,就写多个 package-->
< !-- < package name=""/> -->
< /typeAliases>
< /configuration>
1.2  在spring容器中配置sqlSessionFactory  applicationContext.xml
< beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> < !-- 配置数据源dataSource --> < !-- 加载配置文件 --> < context:property-placeholder location="classpath:db.properties"/> < !-- 数据库连接池 --> < bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> < property name="driverClassName" value="https://www.songbingjia.com/android/${jdbc.driver}"/> < property name="url" value="https://www.songbingjia.com/android/${jdbc.url}"/> < property name="username" value="https://www.songbingjia.com/android/${jdbc.username}"/> < property name="password" value="https://www.songbingjia.com/android/${jdbc.password}"/> < property name="maxActive" value="https://www.songbingjia.com/android/10"/> < property name="maxIdle" value="https://www.songbingjia.com/android/5"/> < /bean> < !-- 配置sqlSessionFactory --> < bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> < !-- 配置数据源 --> < property name="dataSource" ref="dataSource"/> < !-- 加载mybatis的配置文件 --> < property name="configLocation" value="https://www.songbingjia.com/android/classpath:SqlMapConfig.xml"/> < /bean> < !-- 原始dao --> < !-- < bean id="userDao" class="cn.itcast.mybatis.dao.UserDaoImpl"> < property name="sqlSessionFactory"ref="sqlSessionFactory"/> < /bean> --> < !-- mapper代理配置 --> < !--< bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 指定mapper接口 < property name="mapperInterface" value="https://www.songbingjia.com/android/cn.itcast.mybatis.mapper.UserMapper"/> 注入SqlSessionFactory < property name="sqlSessionFactory"ref="sqlSessionFactory"/> < /bean> --> < !-- 使用mapper扫描器创建mapper代理对象 扫描器把自动将包下边的mapper扫描出来创建代理对象在spring容器注册,bean的id为类名(首字母小写) --> < bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> < !-- 指定要扫描的包路径,如果要扫描多个包,中间使用半角逗号分隔 注意:如果使用扫描器,不需要在sqlMapConfig.xml中加载mapper,要将mapper.xml和mapper.java放在同一个目录且同名 --> < property name="basePackage" value="https://www.songbingjia.com/android/cn.itcast.mybatis.mapper"/> < property name="sqlSessionFactoryBeanName" value="https://www.songbingjia.com/android/sqlSessionFactory"> < /property> < /bean> < /beans>

1.3 接口mapper.java和mapper.xml
public interface UserMapper {//根据用户id查询用户信息 public User findUserById(int id)throws Exception; //查询用户使用resultMap public User findUserByIdResultMap(int id)throws Exception; //根据用户名称模糊查询 public List< User> findUserByName(String username)throws Exception; //插入用户 public void insertUser(User user)throws Exception; //更新用户 public void updateUser(User user)throws Exception; }

最好加入sql片段动态抽取重复部分
< ?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"> < !-- 一个mapper映射文件是以sql语句为单位进行配置,最终将sql语句封装到MappedStatement对象中 namespace命名空间作用是更好对sql语句进行隔离,方便管理sql注意:后期讲mybatis的mapper代理开发方式时namespace有特殊的作用,如下: namespace等于mapper接口类路径,这样实现通过映射文件找到对应的mapper接口是哪个 --> < mapper namespace="cn.itcast.mybatis.mapper.UserMapper"> < !-- 打开二级缓存 --> < !-- < cache type="org.mybatis.caches.ehcache.EhcacheCache"/> --> < !-- 根据用户id查询一个用户信息 select:用于查询,主要配置sql语句、输入参数类型、输出结果类型 最终该 select 标签 中所配置的内容会封装到MappedStatement对象,可以将该 select称为是一个Statement id:唯 一标识 namespace下的一个sql语句,将id称为Statement的idparameterType:指定输入参数的类型(简单类型、自定义pojo) #{}:表示一个占位符号,占位符号可以防止sql注入 #{value}:value表示接收输入参数的值,如果接收的输入参数是简单类型,#{}里边可以写value或其它的名称 resultType:将sql查询结果集映射成java对象 将多个列的值映射到一个对象中,需要定义的pojo,resultType映射规则是sql查询列名和pojo的属性名必须一致方可完成映射 resultType 指定单条记录所映射的java对象--> < select id="findUserById" parameterType="int" resultType="user"> SELECT id,username,birthday,sex,addressFROM USER WHERE id = #{id} < /select> < !-- 使用resultMap将列名和pojo的属性值作一个对应关系,完成映射 id:唯一标识 一个元素 type:最终映射的pojo类型 --> < resultMap type="user" id="queryUserResultMap"> < !-- id标识 查询结果集中唯一标识列 column:结果集中唯 一标识 的列名 property:将唯一标识 的列所映射到的type指定的pojo的属性名 --> < id column="id_" property="id"/> < !-- 如果结果集有多个列组合成一个唯 一标识,定义两个id标签 --> < !-- result表示:普通列 --> < result column="username_" property="username"/> < result column="birthday_" property="birthday"/> < result column="sex_" property="sex"/> < result column="address_" property="address"/> < /resultMap> < !-- 查询用户,使用resultMap完成结果映射 --> < select id="findUserByIdResultMap" parameterType="int" resultMap="queryUserResultMap"> SELECT id id_,username username_,birthday birthday_,sex sex_,address address_FROM USER WHERE id = #{id} < /select> < !-- 根据用户名称模糊查询用户信息列表 resultType:不管结果集记录的数量有多少,resutType指定单条记录所映射的java对象 resultType映射规则是sql查询列名和pojo的属性名必须一致方可完成映射 ${}:表示一个sql拼接符号,相当于字符串的拼接: “SELECT * FROM USER WHERE username LIKE \'%” + ${}表示的串 + “%\'” ${}:如果接收输入参数是一个简单类型,${} 中只能写value${}实现sql拼接是无法防止sql注入的。 --> < select id="findUserByName" parameterType="java.lang.String" resultType="cn.itcast.mybatis.po.User"> SELECT * FROM USER WHERE username LIKE \'%${value}%\' < /select> < !-- 添加用户 需要输入参数是多个值,如果传入简单类型是无法满足要求。 输入参数类型可以定义为pojo(cn.itcast.mybatis.po.User包括多个属性) #{}如何获取对象的值? #{}是通过OGNL读取对象的值,OGNL的表达式方式:属性.属性.属性。。。。直到把对象中的属性值读取过来 过止 mysql数据库通过select LAST_INSERT_ID(); 获取自增主键的值,在insert语句执行之后去执行LAST_INSERT_ID()获取新记录的主键 --> < insert id="insertUser" parameterType="cn.itcast.mybatis.po.User"> < !-- keyProperty:将主键值设置到输入参数的哪个属性,设置到user的id属性中 order:selectkey中的sql语句在insert语句执行的前或后,这里要设置成"AFTER" resultType:select LAST_INSERT_ID()查询出的值 --> < selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> select LAST_INSERT_ID() < /selectKey> insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address}); < /insert> < !-- 使用mysql的uuid生成主键 --> < !-- < insert id="insertUser" parameterType="cn.itcast.mybatis.po.User"> keyProperty:将主键值设置到输入参数的哪个属性,设置到user的id属性中 order:select uuid()在insert执行之前去执行得到uuid作为主键,将主键值设置到user的属性中 resultType:select uuid()查询出的值< selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String"> select uuid() < /selectKey> insert into user(id,username,birthday,sex,address) values(#{id},#{username},#{birthday},#{sex},#{address}); < /insert> --> < !-- 修改用户--> < update id="updateUser" parameterType="cn.itcast.mybatis.po.User"> update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id} < /update> < !-- 删除用户 --> < delete id="deleteUser" parameterType="int"> delete from user where id = #{id} < /delete> < /mapper>

1.4  spring与mybatis整合生成代理对象方法2 扫描器
【Mybatis 和Spring整合之mapper代理开发】
Mybatis 和Spring整合之mapper代理开发

文章图片

 

    推荐阅读