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代理开发的更多相关文章

  1. Mybatis 和Spring整合之原始dao开发

    F:\Aziliao\mybatis\代码\31.mybatis与spring整合-开发原始dao 1.1. SqlMapConfig.xml <?xml version="1.0&q ...

  2. (十四)mybatis 和 spring 整合

    目录 整合思想 整合步骤 整合之后原始 dao 开发 整合之后 Mapper 代理开发 总结 整合思想 让 spring 管理 sqlSessionFactory ,使用 单例模式 创建该对象 : 根 ...

  3. Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合

    Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合 标签: mybatisSpringbeanApplicationContextMapper 2015-12-31 1 ...

  4. Spring+SpringMVC+MyBatis深入学习及搭建(二)——MyBatis原始Dao开发和mapper代理开发

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6869133.html 前面有写到Spring+SpringMVC+MyBatis深入学习及搭建(一)——My ...

  5. Mybatis的mapper代理开发dao方法

    看完了之前的mybatis原始的dao开发方法是不是觉得有点笨重,甚至说没有发挥mybatis 作为一个框架的优势.总结了一下,原始的dao方法有以下几点不足之处 dao接口实现方法中存在大量的模板方 ...

  6. 【转】Mybatis学习---MyBatis知识、原始Dao开发和mapper代理开发

    [原文]https://www.toutiao.com/i6594610137560777223/ 一.什么是MyBatis MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及 ...

  7. mybatis入门基础(二)----原始dao的开发和mapper代理开发

    承接上一篇 mybatis入门基础(一) 看过上一篇的朋友,肯定可以看出,里面的MybatisService中存在大量的重复代码,看起来不是很清楚,但第一次那样写,是为了解mybatis的执行步骤,先 ...

  8. 【mybatis深度历险系列】深入浅出mybatis中原始dao的开发和mapper代理开发

    使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper接口开发方法.mybatis在进行dao开发的时候,涉及到三姐妹,分别是SqlSessionFactoryBuilder ...

  9. Mybatis学习总结(二)——Mapper代理开发

    一.概要 1.原始DAO开发中存在的问题:(1)DAO实现方法体中存在很多过程性代码.(2)调用SqlSession的方法(select/insert/update)需要指定Statement的id, ...

随机推荐

  1. JS实现图片预览与等比缩放

    案例仅为图片预览功能,省略图片上传步骤,框架为easyui. HTML代码: @*text-align:center;水平居中 vertical-align: middle;display: tabl ...

  2. 另一个C#模拟post请求的例子

    private string returninstallTmnl(AddTmnlInstallParameter model) { string url = ConfigurationSettings ...

  3. JQuery对数组的一些操作总结

    JQuery对数组的处理非常便捷并且功能强大齐全,一步到位的封装了很多原生js数组不能企及的功能.下面来看看JQuery数组的强大之处在哪. $.each(array, [callback]) 遍历 ...

  4. FLASK日志记录

    from flask import Flask from flask_restful import Resource, Api import logging app = Flask(__name__) ...

  5. php扩展库

    php调用C/C++动态链接库 字数997 阅读28 评论0 喜欢0 本人最近在找实习,移动开发方向.有意者可直接与本人联系.谢谢! 一.简介 一般而言,php速度已经比较快,但是,对于一些较高级开发 ...

  6. base64的编码解码的一些坑

    1. //编码 value = base64encode(utf16to8(src)) //解码 value = utf8to16(base64decode(src)) 这里:base64编码之前先转 ...

  7. JavaScript & jQuery Code Snippet

    1. 按照每个object的Name属性对object对象集合进行排序: //sort a collection of objects by Name property function sortBy ...

  8. 使用SlidingPaneLayout 实现仿微信的滑动返回

    上周,公司的项目改版要求加上一个右滑返回上一个界面,于是就在网上找了一些开源库打算实现.但是在使用的时候遇见了许多的问题.试了两天用过 https://github.com/ikew0ng/Swipe ...

  9. Python类型总结

    python 中处处是类的实例化 a=1 存储的是数字类型 而b='123'是字符串类型 []:读写方便的一张类型 True判断真假的一组{}键值对形式 class:则是描述静态元素和动态元素的结合体 ...

  10. java、C语言实现数组模拟栈

    java: public class ArrayStack { private int[] data; private int top; private int size; public ArrayS ...