Mybatis 和Spring整合之mapper代理开发
F:\1ziliao\mybatis\代码

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="true"/>
<!-- 设置为false实现按需求加载 -->
<setting name="aggressiveLazyLoading" value="false"/>
<!-- 开启二级缓存 -->
<setting name="cacheEnabled" value="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="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="10"/>
<property name="maxIdle" value="5"/>
</bean> <!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 加载mybatis的配置文件 -->
<property name="configLocation" value="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="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="cn.itcast.mybatis.mapper"/>
<property name="sqlSessionFactoryBeanName" value="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的id parameterType:指定输入参数的类型(简单类型、自定义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,address FROM 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整合之原始dao开发
F:\Aziliao\mybatis\代码\31.mybatis与spring整合-开发原始dao 1.1. SqlMapConfig.xml <?xml version="1.0&q ...
- (十四)mybatis 和 spring 整合
目录 整合思想 整合步骤 整合之后原始 dao 开发 整合之后 Mapper 代理开发 总结 整合思想 让 spring 管理 sqlSessionFactory ,使用 单例模式 创建该对象 : 根 ...
- Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合
Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合 标签: mybatisSpringbeanApplicationContextMapper 2015-12-31 1 ...
- Spring+SpringMVC+MyBatis深入学习及搭建(二)——MyBatis原始Dao开发和mapper代理开发
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6869133.html 前面有写到Spring+SpringMVC+MyBatis深入学习及搭建(一)——My ...
- Mybatis的mapper代理开发dao方法
看完了之前的mybatis原始的dao开发方法是不是觉得有点笨重,甚至说没有发挥mybatis 作为一个框架的优势.总结了一下,原始的dao方法有以下几点不足之处 dao接口实现方法中存在大量的模板方 ...
- 【转】Mybatis学习---MyBatis知识、原始Dao开发和mapper代理开发
[原文]https://www.toutiao.com/i6594610137560777223/ 一.什么是MyBatis MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及 ...
- mybatis入门基础(二)----原始dao的开发和mapper代理开发
承接上一篇 mybatis入门基础(一) 看过上一篇的朋友,肯定可以看出,里面的MybatisService中存在大量的重复代码,看起来不是很清楚,但第一次那样写,是为了解mybatis的执行步骤,先 ...
- 【mybatis深度历险系列】深入浅出mybatis中原始dao的开发和mapper代理开发
使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper接口开发方法.mybatis在进行dao开发的时候,涉及到三姐妹,分别是SqlSessionFactoryBuilder ...
- Mybatis学习总结(二)——Mapper代理开发
一.概要 1.原始DAO开发中存在的问题:(1)DAO实现方法体中存在很多过程性代码.(2)调用SqlSession的方法(select/insert/update)需要指定Statement的id, ...
随机推荐
- 让div铺满整个空间
需要用到几个css属性: .content{ width:100%;position: absolute;top: 50px;bottom: 0px;left: } 设置了bottom.top及abs ...
- <td>标签scope属性
HTML <td> 标签的 scope 属性 HTML <td> 标签 实例 下面的例子把两个 th 元素标识为列的表头,把两个 td 元素标识为行的表头: <table ...
- mac 好用软件地址存储
Navicat Premium 12.0.24 for mac已破解中文 https://www.52pojie.cn/thread-727433-1-1.html sublime 破解方法https ...
- Cardinality Estimation算法学习(一)(了解基数计算的基本概念及回顾求字符串中不重复元素的个数的问题)
最近在菜鸟教程上自学redis.看到Redis HyperLogLog的时候,对“基数”以及其它一些没接触过(或者是忘了)的东西产生了好奇. 于是就去搜了“HyperLogLog”,从而引出了Card ...
- package和package-lock区别;dependencies和devDependencies区别
package和package-lock package.json: 主要用来定义项目中需要依赖的包 package-lock.json: 在 npm install时候生成一份文件,用以记录当前状态 ...
- JavaEE之servlet相关技术
相关技术:为了灵活实现的不同路径(/hello)执行不同的资源( HeIIoMyServlet)我们需要使用XML进行配置;为了限定XML内容,我们需要使用xml约束(DTD或schema);为了获得 ...
- csharp: using using System.Web.Script.Serialization read json
using System; using System.Data; using System.Configuration; using System.Collections; using System. ...
- js 密码 正则表达式
1. 代码 function checkPassword(str){ var reg1 = /[!@#$%^&*()_?<>{}]{1}/; var reg2 = /([a-zA- ...
- Scrapy爬虫requests
requests 模块 模块的由来: 浏览器可以浏览网站, 是由于浏览器发送了requests , 各种请求.打开一个网站可能有几十到几百个请求. 从而服务器端会反馈各种因应不同请求生成的数据. 我们 ...
- 医药箱APP静态小项目
花费了10天时间,纯手写一个医药箱APP静态小项目,里面有上拉加载.左右滑动.弹出层淡入淡出等效果,主要是练习. 以下是一部分页面效果图: 我用的是谷歌的开发者工具的手机端模拟器. 里面需要优化的地方 ...