Spring和Mybatis整合-mapper开发
接着上篇文章 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 全局配置文件
- <?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>
- </configuration>
我们这里不需要添加 mybatis 配置,因为目前所有的配置(数据库连接,添加映射)都让 spring 做了。其中添加映射也去掉了,是因为在 spring 配置文件中开启了mapper批量扫描,待会儿有介绍。
2、applicationContext.java spring 的配置文件
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- 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.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx.xsd">
- <!--加载配置文件-->
- <context:property-placeholder location="classpath:db.properties"/>
- <!--数据源,配置c3p0连接池-->
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <!--注入属性-->
- <property name="driverClass" value="${jdbc.driver}"></property>
- <property name="jdbcUrl" value="${jdbc.url}"></property>
- <property name="user" value="${jdbc.username}"></property>
- <property name="password" value="${jdbc.password}"></property>
- </bean>
- <!--sqlSessionFactory-->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <!--加载mybatis配置文件-->
- <property name="configLocation" value="mybatis/Configuration.xml"/>
- <!--配置数据源,ref和数据源的id一致-->
- <property name="dataSource" ref="dataSource"/>
- </bean>
- <!--mapper批量扫描-->
- <!--
- mapper批量扫描,从mapper包中扫描出mapper接口,自动创建代理对象并且在spring容器中注册
- 遵循规范:将mapper.java和mapper.xml映射文件保持一致,且在一个目录中
- 自动扫描出来的mapper的bean的id和mapper类名(首字母小写)
- -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <!--指定扫描的包名-->
- <property name="basePackage" value="com.liuyanzhao.ssm.mapper" />
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
- </bean>
- </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
- jdbc.driver=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8
- jdbc.username=root
- jdbc.password=
之所以把 数据库连接信息从 applicationContext.xml 中分离出来,是因为以后我们的 xml 文件会越来越多,需要重新修改,甚至是让机器来修改 我们的数据库用户名和密码等信息,十分不方便。
4、UserMapper.java
- package com.liuyanzhao.ssm.mapper;
- import com.liuyanzhao.ssm.po.User;
- /**
- * Created by 言曌 on 2017/8/10.
- */
- public interface UserMapper {
- //根据id查询用户信息
- public User findUserById(int id) throws Exception;
- }
5、UserMapper.xml
- <?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 namespace="com.liuyanzhao.ssm.mapper.UserMapper">
- <!--查询用户-->
- <select id="findUserById" parameterType="int" resultType="com.liuyanzhao.ssm.po.User">
- SELECT * FROM user WHERE id=#{value}
- </select>
- </mapper>
6、User.java User的持久类
- package com.liuyanzhao.ssm.po;
- import java.util.Date;
- /**
- * 用户的持久类
- */
- public class User {
- private int id; //编号
- private String username; //用户名
- private String gender; //性别
- private Date birthday; //生日
- private String address; //地址
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getGender() {
- return gender;
- }
- public void setGender(String gender) {
- this.gender = gender;
- }
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- @Override
- public String toString() {
- return "User{" +
- "id=" + id +
- ", username='" + username + '\'' +
- ", gender='" + gender + '\'' +
- ", birthday=" + birthday +
- ", address='" + address + '\'' +
- '}';
- }
- }
7、测试类 UserMapperTest.java
- package com.liuyanzhao.ssm.test;
- import com.liuyanzhao.ssm.mapper.UserMapper;
- import com.liuyanzhao.ssm.po.User;
- import org.junit.Before;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- /**
- * Created by 言曌 on 2017/8/14.
- */
- public class UserMapperTest {
- private ApplicationContext applicationContext;
- //在 setUp方法中得到spring容器
- @Before
- public void setUp() throws Exception{
- applicationContext =
- new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
- }
- @Test
- public void testFindUserById() throws Exception {
- UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");
- //调用userMapper的方法
- User user = userMapper.findUserById(29);
- System.out.println(user);
- }
- }
以上代码已打包:
链接: https://pan.baidu.com/s/1gf6Ckx1 密码: hbqd
本文链接:https://liuyanzhao.com/5893.html
Spring和Mybatis整合-mapper开发相关推荐
- (八)Spring与MyBatis整合
持久层 目录 Mybatis 开发步骤回顾 Mybatis 开发中存在的问题 Spring 与 Mybatis 整合思路 Spring 与 Mybatis 整合的开发步骤 Spring 与 Mybat ...
- 【Spring 持久层】Spring 与 Mybatis 整合
持久层 持久层整合总述 Mybatis 开发步骤回顾 Mybatis 开发中存在的问题 Spring 与 Mybatis 整合思路 Spring 与 Mybatis 整合的开发步骤 Spring 与 ...
- java元婴期(26)----java进阶(mybatis(5)---spring和mybatis整合(重点)逆向工程(会用))
spring和mybatis整合 1.整合思路 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用SqlSessionFactor ...
- Spring boot Mybatis 整合(注解版)
之前写过一篇关于springboot 与 mybatis整合的博文,使用了一段时间spring-data-jpa,发现那种方式真的是太爽了,mybatis的xml的映射配置总觉得有点麻烦.接口定义和映 ...
- Spring boot Mybatis 整合(完整版)
Spring boot Mybatis 整合(完整版) 更多干货 SpringBoot系列目录 正题 本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 springboot: ...
- 基于Spring+SpringMVC+Mybatis分布式敏捷开发系统架构zhen项目
原项目链接:https://github.com/shuzheng/zheng 目前看过做完整的项目,大家一起学习一下,详情请点击原项目链接的地址 前言 zheng项目创建于2016年10月4日,正在 ...
- Spring、Mybatis整合Service优化思路,DAO层、Service层最终编码以及log4j日志的使用
5. Spring.Mybatis整合Service层事务控制优化思路分析 # spring中处理事务的两种方式1. 编程式事务处理定义:通过在业务层中注入事务管理器对象,然后通过编码的方式进行事务控 ...
- Spring、mybatis整合,阿里短信接口
mybatis-spring整合 一.MyBatis与Spring的集成 在学习mybatis配置时,对于mybatis-config配置的时候我们发现,大致是需要配置三个方面:setting.dat ...
- Spring与Mybatis整合
一. MyBatis与Spring的集成 在学习mybatis配置时,对于mybatis-config配置的时候我们发现,大致是需要配置三个方面:setting.datasource.mappers ...
- 基于Spring+SpringMVC+Mybatis分布式敏捷开发系统架构---权限管理系统
原文链接: https://github.com/shuzheng/zheng 安利一个GitHub上的项目,见原文链接. 基于Spring+SpringMVC+Mybatis分布式敏捷开发系统架构, ...
最新文章
- nginx内置变量 大全
- MmGetSystemRoutineAddress和MiFindExportedRoutineByName函数的实现代码
- 在java中将数据信息写入文本中(2)
- 通俗易懂!白话朴素贝叶斯
- spring自动注入--------
- [导入]ASP.NET 2.0中Page事件的执行顺序
- 开发指南专题七:JEECG微云快速开发平台查询HQL过滤器
- Java限流之 —— Guawa
- flinksql写入hudi 踩坑实录
- Spring4.x(2)--SpringIOC的概念和作用
- Java常见面试题:BIO,NIO,AIO 有什么区别?
- 数据可视化图表的类型和特点
- 三年级计算机测试题,小学三年级信息技术考试试题
- 【论文阅读】Speicher: Securing LSM-based Key-Value Stores using Shielded Execution
- pdf转word完全免费软件
- Coolpad/酷派炫影5890 root教程_方法
- python tokenize_model_AttributeError:“module”对象没有属性“tokenize”
- Google 常用镜像收集
- CSP J/S SH 迷惑行为大赏
- PS制作立体效果——圆环