思路: SqlSessionFactory -> SqlSession ->StudentMapper ->CRUD 可以发现,MyBatis最终是通过SqlSessionFactory来操作数据库, Spring整合MyBatis其实就是将MyBatis的SqlSessionFactory交给Spring
SM整合步骤:
jar mybatis-spring. jar spring- tx. jar spring- jdbc. jar spring- -expression. jar spr ing- context- support. jar spring- -core. jar spring- -context. jar spr ing- -beans. jar spring- aop. jar spring- web. jar commons- logging. jar
commons-dbcp. jar
ojdbc. jar
mybatis. jar
log4j. jar
commons - pool. jar
2.类-表
3.MyBatis配置文件conf. xml
4.通过mapper. xml将类、表建立映射关系
5.之前使mybatis:用conf.xml ->SqlSessionFacotry
现在整合的时候,需要通过Spring管理SqlSessionFacotry,因此产生sqlSessionFacotry 所需要的数据库信息不在放入conf.xml而需要放入spring配置文件中
配置Spring配置文件(applicationContext. xml )
6.使用Spring-MyBatis整合产物开发程序
目标:通过spring产 生mybatis最终操作需要的动态mapper对 象(StudentMapper对象)
Spring产生动态mapper对象有三种方法: a. DAO层实现类继承SqlSessionDaoSupport类
SqlSessionDaoSupport类提供了一个 属性SqlSession
项目结构 ![image-20220128223004647](D:\bloglocal\Spring - MyBatis.assets\image-20220128223004647.png)
applicationContext.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns ="http://www.springframework.org/schema/beans" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <bean id ="config" class ="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer" > <property name ="locations" > <array > <value > classpath:db.properties</value > </array > </property > </bean > <bean id ="dataSource" class ="org.apache.commons.dbcp2.BasicDataSource" > <property name ="url" value ="${url}" > </property > <property name ="username" value ="${username}" > </property > <property name ="password" value ="${password}" > </property > <property name ="driverClassName" value ="${driver}" > </property > <property name ="maxIdle" value ="${maxIdle}" > </property > </bean > <bean id ="sqlSessionFactoryBean" class ="org.mybatis.spring.SqlSessionFactoryBean" > <property name ="dataSource" ref ="dataSource" > </property > <property name ="mapperLocations" value ="club/shyee/mapper/*.xml" > </property > </bean > <bean class ="org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name ="sqlSessionFactoryBeanName" value ="sqlSessionFactoryBean" > </property > <property name ="basePackage" value ="club.shyee.mapper,club.shyee.dao" > </property > </bean > <bean id ="userService" class ="club.shyee.service.impl.UserServiceImpl" > <property name ="userMapper" ref ="userMapper" > </property > </bean > </beans >
采用第三种方式,不走Dao层,人家直接写好了。
而且不用conf.xml,因为直接加载到IOC中了
db.properties
1 2 3 4 5 driver =com.mysql.cj.jdbc.Driver url =url username =user password =password maxIdle =1000
entity 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 package club.shyee.entity;public class User {private int uId;private String uName;private String uPass;private String phone;private String email;public int getuId () { return uId; } public void setuId (int uId) { this .uId = uId; } public String getuName () { return uName; } public void setuName (String uName) { this .uName = uName; } public String getuPass () { return uPass; } public void setuPass (String uPass) { this .uPass = uPass; } public String getPhone () { return phone; } public void setPhone (String phone) { this .phone = phone; } public String getEmail () { return email; } public void setEmail (String email) { this .email = email; } @Override public String toString () { return "User [uId=" + uId + ", uName=" + uName + ", uPass=" + uPass + ", phone=" + phone + ", email=" + email + "]" ; } public User (int uId, String uName, String uPass, String phone, String email) { super (); this .uId = uId; this .uName = uName; this .uPass = uPass; this .phone = phone; this .email = email; } public User () { super (); } }
mapper UserMapper.java
1 2 3 4 5 6 7 8 package club.shyee.mapper;import club.shyee.entity.User;public interface UserMapper { public void queryUserById (int uId) ; public void addUser (User user) ; }
userMapper.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <?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 ="club.shyee.mapper.UserMapper" > <select id ="queryUserById" parameterType ="int" resultType ="club.shyee.entity.User" > select * from user where uId=${uId} </select > <insert id ="addUser" parameterType ="club.shyee.entity.User" > insert into user(uName,uPass,phone,email) values(#{uName},#{uPass},#{phone},#{email}) </insert > </mapper >
service 接口
1 2 3 4 5 6 7 package club.shyee.service;import club.shyee.entity.User;public interface IUserService { public void addUser (User user) ; }
实现类
set方法必要
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package club.shyee.service.impl;import club.shyee.entity.User;import club.shyee.mapper.UserMapper;import club.shyee.service.IUserService;public class UserServiceImpl implements IUserService { private UserMapper userMapper; public UserMapper getUserMapper () { return userMapper; } public void setUserMapper (UserMapper userMapper) { this .userMapper = userMapper; } @Override public void addUser (User user) { System.out.println("Service 实现类中的User" +user); userMapper.addUser(user); } }
测试 写一个测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package club.shyee.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import club.shyee.entity.User;import club.shyee.service.IUserService;public class Test1 { public static void main (String[] args) { System.out.println("测试" ); test1(); } public static void test1 () { ApplicationContext context=new ClassPathXmlApplicationContext ("classpath:applicationContext.xml" ); IUserService userService = (IUserService)context.getBean("userService" ); User user=new User (1 ,"老刘" ,"12596874" ,"14523654254" ,"ttt@qq.com" ); userService.addUser(user); } }
我放了两个打印,一个在service层一个在Dao层,会发现,application Context会自动写好了Dao的过程,所以不会打印出dao层的数据。即不用设计dao层。