接着上篇文章 Spring和Mybatis整合-原生dao开发 写。

一、开发准备

1、jar 包

包括 spring开发必备包,mybatis核心包,spring和mybatis整合包,日志包

还有 Junit4 包

2、数据库

我在本地测试,主机 localhost,用户名 root,密码为空

创建数据表 user,5个字段,测试数据如下

二、文件结构

1、文件夹结构

config 存放配置文件

----mybatis 存放mybatis的配置文件

----spring    存放spring的配置文件

----db.properties  数据库连接属性

src  存放源代码

----com.liuyanzhao.smm 包名

--------mapper 存放mapper类和mapper.xml(映射文件)

--------po         存放持久类

--------test        存放测试类

本例文件一览

其中 jar 包放在 web/WEB_INF/lib 文件夹中,并已导入 环境中

三、代码实现

应重视的文件,这里会高亮标识

1、Configuration.xml   mybatis 全局配置文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. </configuration>

我们这里不需要添加 mybatis 配置,因为目前所有的配置(数据库连接,添加映射)都让 spring 做了。其中添加映射也去掉了,是因为在 spring 配置文件中开启了mapper批量扫描,待会儿有介绍。

2、applicationContext.java   spring 的配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx.xsd">
  15. <!--加载配置文件-->
  16. <context:property-placeholder location="classpath:db.properties"/>
  17. <!--数据源,配置c3p0连接池-->
  18. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  19. <!--注入属性-->
  20. <property name="driverClass" value="${jdbc.driver}"></property>
  21. <property name="jdbcUrl" value="${jdbc.url}"></property>
  22. <property name="user" value="${jdbc.username}"></property>
  23. <property name="password" value="${jdbc.password}"></property>
  24. </bean>
  25. <!--sqlSessionFactory-->
  26. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  27. <!--加载mybatis配置文件-->
  28. <property name="configLocation" value="mybatis/Configuration.xml"/>
  29. <!--配置数据源,ref和数据源的id一致-->
  30. <property name="dataSource" ref="dataSource"/>
  31. </bean>
  32. <!--mapper批量扫描-->
  33. <!--
  34. mapper批量扫描,从mapper包中扫描出mapper接口,自动创建代理对象并且在spring容器中注册
  35. 遵循规范:将mapper.java和mapper.xml映射文件保持一致,且在一个目录中
  36. 自动扫描出来的mapper的bean的id和mapper类名(首字母小写)
  37. -->
  38. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  39. <!--指定扫描的包名-->
  40. <property name="basePackage" value="com.liuyanzhao.ssm.mapper" />
  41. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  42. </bean>
  43. </beans>

先要创建数据源(dataSource),然后获得 连接工厂(sqlSessionFactory),最终得以配置。

本例 (mapper开发)的代码优化过一次,以前是 每一个 mapper,我们就要添加一个 bean,像这样

同时,还需要在 mybatis 的全局配置文件中添加映射

现在,当我们添加了 mapper批量扫描 代码,spring会帮我们做很多事情。

mapper批量扫描,从mapper包中扫描出mapper接口,自动创建代理对象并且在spring容器中注册

遵循规范:将mapper.java和mapper.xml映射文件保持一致,且在一个目录中

自动扫描出来的mapper的bean的id和mapper类名(首字母小写)

同时,这里推荐使用mapper批量扫描

3、db.properties

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8
  3. jdbc.username=root
  4. jdbc.password=

之所以把 数据库连接信息从 applicationContext.xml 中分离出来,是因为以后我们的 xml 文件会越来越多,需要重新修改,甚至是让机器来修改 我们的数据库用户名和密码等信息,十分不方便。

4、UserMapper.java

  1. package com.liuyanzhao.ssm.mapper;
  2. import com.liuyanzhao.ssm.po.User;
  3. /**
  4. * Created by 言曌 on 2017/8/10.
  5. */
  6. public interface UserMapper {
  7. //根据id查询用户信息
  8. public User findUserById(int id) throws Exception;
  9. }

5、UserMapper.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.liuyanzhao.ssm.mapper.UserMapper">
  6. <!--查询用户-->
  7. <select id="findUserById" parameterType="int" resultType="com.liuyanzhao.ssm.po.User">
  8. SELECT * FROM user WHERE id=#{value}
  9. </select>
  10. </mapper>

6、User.java    User的持久类

  1. package com.liuyanzhao.ssm.po;
  2. import java.util.Date;
  3. /**
  4. * 用户的持久类
  5. */
  6. public class User {
  7. private int id; //编号
  8. private String username; //用户名
  9. private String gender; //性别
  10. private Date birthday; //生日
  11. private String address; //地址
  12. public int getId() {
  13. return id;
  14. }
  15. public void setId(int id) {
  16. this.id = id;
  17. }
  18. public String getUsername() {
  19. return username;
  20. }
  21. public void setUsername(String username) {
  22. this.username = username;
  23. }
  24. public String getGender() {
  25. return gender;
  26. }
  27. public void setGender(String gender) {
  28. this.gender = gender;
  29. }
  30. public Date getBirthday() {
  31. return birthday;
  32. }
  33. public void setBirthday(Date birthday) {
  34. this.birthday = birthday;
  35. }
  36. public String getAddress() {
  37. return address;
  38. }
  39. public void setAddress(String address) {
  40. this.address = address;
  41. }
  42. @Override
  43. public String toString() {
  44. return "User{" +
  45. "id=" + id +
  46. ", username='" + username + '\'' +
  47. ", gender='" + gender + '\'' +
  48. ", birthday=" + birthday +
  49. ", address='" + address + '\'' +
  50. '}';
  51. }
  52. }

7、测试类 UserMapperTest.java

  1. package com.liuyanzhao.ssm.test;
  2. import com.liuyanzhao.ssm.mapper.UserMapper;
  3. import com.liuyanzhao.ssm.po.User;
  4. import org.junit.Before;
  5. import org.junit.Test;
  6. import org.springframework.context.ApplicationContext;
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;
  8. /**
  9. * Created by 言曌 on 2017/8/14.
  10. */
  11. public class UserMapperTest {
  12. private ApplicationContext applicationContext;
  13. //在 setUp方法中得到spring容器
  14. @Before
  15. public void setUp() throws  Exception{
  16. applicationContext =
  17. new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
  18. }
  19. @Test
  20. public void testFindUserById() throws Exception {
  21. UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");
  22. //调用userMapper的方法
  23. User user = userMapper.findUserById(29);
  24. System.out.println(user);
  25. }
  26. }

以上代码已打包:

链接: https://pan.baidu.com/s/1gf6Ckx1 密码: hbqd

本文链接:https://liuyanzhao.com/5893.html

Spring和Mybatis整合-mapper开发相关推荐

  1. (八)Spring与MyBatis整合

    持久层 目录 Mybatis 开发步骤回顾 Mybatis 开发中存在的问题 Spring 与 Mybatis 整合思路 Spring 与 Mybatis 整合的开发步骤 Spring 与 Mybat ...

  2. 【Spring 持久层】Spring 与 Mybatis 整合

    持久层 持久层整合总述 Mybatis 开发步骤回顾 Mybatis 开发中存在的问题 Spring 与 Mybatis 整合思路 Spring 与 Mybatis 整合的开发步骤 Spring 与 ...

  3. java元婴期(26)----java进阶(mybatis(5)---spring和mybatis整合(重点)逆向工程(会用))

    spring和mybatis整合 1.整合思路 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用SqlSessionFactor ...

  4. Spring boot Mybatis 整合(注解版)

    之前写过一篇关于springboot 与 mybatis整合的博文,使用了一段时间spring-data-jpa,发现那种方式真的是太爽了,mybatis的xml的映射配置总觉得有点麻烦.接口定义和映 ...

  5. Spring boot Mybatis 整合(完整版)

    Spring boot Mybatis 整合(完整版) 更多干货 SpringBoot系列目录 正题 本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 springboot: ...

  6. 基于Spring+SpringMVC+Mybatis分布式敏捷开发系统架构zhen项目

    原项目链接:https://github.com/shuzheng/zheng 目前看过做完整的项目,大家一起学习一下,详情请点击原项目链接的地址 前言 zheng项目创建于2016年10月4日,正在 ...

  7. Spring、Mybatis整合Service优化思路,DAO层、Service层最终编码以及log4j日志的使用

    5. Spring.Mybatis整合Service层事务控制优化思路分析 # spring中处理事务的两种方式1. 编程式事务处理定义:通过在业务层中注入事务管理器对象,然后通过编码的方式进行事务控 ...

  8. Spring、mybatis整合,阿里短信接口

    mybatis-spring整合 一.MyBatis与Spring的集成 在学习mybatis配置时,对于mybatis-config配置的时候我们发现,大致是需要配置三个方面:setting.dat ...

  9. Spring与Mybatis整合

    一. MyBatis与Spring的集成 在学习mybatis配置时,对于mybatis-config配置的时候我们发现,大致是需要配置三个方面:setting.datasource.mappers ...

  10. 基于Spring+SpringMVC+Mybatis分布式敏捷开发系统架构---权限管理系统

    原文链接: https://github.com/shuzheng/zheng 安利一个GitHub上的项目,见原文链接. 基于Spring+SpringMVC+Mybatis分布式敏捷开发系统架构, ...

最新文章

  1. nginx内置变量 大全
  2. MmGetSystemRoutineAddress和MiFindExportedRoutineByName函数的实现代码
  3. 在java中将数据信息写入文本中(2)
  4. 通俗易懂!白话朴素贝叶斯
  5. spring自动注入--------
  6. [导入]ASP.NET 2.0中Page事件的执行顺序
  7. 开发指南专题七:JEECG微云快速开发平台查询HQL过滤器
  8. Java限流之 —— Guawa
  9. flinksql写入hudi 踩坑实录
  10. Spring4.x(2)--SpringIOC的概念和作用
  11. Java常见面试题:BIO,NIO,AIO 有什么区别?
  12. 数据可视化图表的类型和特点
  13. 三年级计算机测试题,小学三年级信息技术考试试题
  14. 【论文阅读】Speicher: Securing LSM-based Key-Value Stores using Shielded Execution
  15. pdf转word完全免费软件
  16. Coolpad/酷派炫影5890 root教程_方法
  17. python tokenize_model_AttributeError:“module”对象没有属性“tokenize”
  18. Google 常用镜像收集
  19. CSP J/S SH 迷惑行为大赏
  20. PS制作立体效果——圆环

热门文章

  1. qt开发用的人多吗_新房认筹猫腻多!开发商只是为达到这个目的才认筹,你入坑了吗?...
  2. fileboy v1.9 发布,文件变更监听通知系统
  3. 程序员在简书|努力奔跑
  4. 再也不学AJAX了!(三)跨域获取资源 ② - JSONP CORS
  5. python简单查询用户
  6. android 中使用svg
  7. 计算机视觉和机器学习,代码,论文大全
  8. 灰度图像加性噪声污染和运动模糊图像复原
  9. 卷积神经网络CNN的由来,为什么要用卷积?
  10. Unity:一键移除所有预制体上的Missing脚本