Spring和Mybatis整合-mapper开发
来源:互联网 发布:文本相似算法 编辑:程序博客网 时间:2024/05/16 10:30
接着上篇文章 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整合-mapper开发
- mybatis和spring整合开发(mapper代理)
- Mybatis-Spring整合Mapper代理开发
- Mybatis和Spring整合mapper代理
- Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合
- Spring 整合Mybatis mapper问题
- Mybatis和Spring整合(原始Dao)非mapper代理
- spring和mybatis整合的关键:org.mybatis.spring.mapper.MapperScannerConfigurer 类作用
- mybatis整合spring(mapper+原始dao)
- Mybatis之Spring整合Mapper代理
- Spring 整合 MyBatis,Mapper注入失败
- 【Mybatis从0到1-016】Spring与MyBatis整合mapper开发DAO(推荐使用)
- Spring和Mybatis整合-原生dao开发
- (五)mybatis学习之mapper代理开发方式及与spring整合
- MyBatis 整合 Spring开发
- mybatis与spring3.1整合(org.mybatis.spring.mapper.MapperFactoryBean)
- mybatis与spring3.1整合(org.mybatis.spring.mapper.MapperFactoryBean)
- Linux内存管理
- Spring和Mybatis整合-原生dao开发
- w3eview
- [Leetcode][python]Max Points on a Line
- linux-线程/进程通信eventfd
- Spring和Mybatis整合-mapper开发
- 基本数据类型
- 一个完整的Java Web项目需要掌握哪些技术
- NOIP2017 D2T1 奶酪
- classpath路径浅谈
- SpringMVC入门程序-以注解方式
- AI当歌唱比赛评委,黑幕将越来越少
- Django框架全面讲解 -- 序列化
- mybatis逆向工程利用mybatis-generator-core自动生成代码