以前Spring和Mybatis整合时,访问数据时是使用硬编码的方式连接数据里对数据进行CRUD,这样用到的对象较多,且不易维护,重复性代码也很多
SqlSessionBuileer ===> SqlSessionFactoty =====> SqlSession =====> CRUD(增删改查)
现在我们直接将这些对象以Bean的方式,配置在Spring配置文件中,并且将它们之间的关系建立好,最后只需通过getBean得到某个service的实现类便能执行CRUD,相当于将mybatis-config.xml中的配置转移至了applicationContext.xml配置文件中
基本步骤 (暂不使用注解实现)
1.导入jar包,以maven项目为例
2.建立开发目录结构,创建实体类
3.创建数据访问接口
4.配置sql映射文件
5.配置Mybatis文件(如果有映射类名等操作可放在Mybatis-config.xml中配置)
6.配置appplicationContext.xml文件
7.测试
1.pom.xml导入jar包,如果是普通web项目,从官网下载jar包导入即可,在spring导入的jar包之上再导入这些jar包,Spring的jar包未 展示
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
2.建立开发目录结构,创建实体类
package cn.einblatt.ssm.entity;
import java.util.Date;
import java.util.List;
public class User {
private Integer id; //id
private String userCode; //用户编码
private String userName; //用户名称
private String userPassword; //用户密码
private Integer gender; //性别
private Date birthday; //出生日期
private String phone; //电话
private String address; //地址
private Integer userRole; //用户角色
private Integer createdBy; //创建者
private Date creationDate; //创建时间
private Integer modifyBy; //更新者
private Date modifyDate; //更新时间
private Role role;
private List<Address> addressList;
public List<Address> getAddressList() {
return addressList;
}
public void setAddressList(List<Address> addressList) {
this.addressList = addressList;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public void setAge(Integer age) {
this.age = age;
}
private Integer age;//年龄
private String userRoleName; //用户角色名称
public String getUserRoleName() {
return userRoleName;
}
public void setUserRoleName(String userRoleName) {
this.userRoleName = userRoleName;
}
public Integer getAge() {
/*long time = System.currentTimeMillis()-birthday.getTime();
Integer age = Long.valueOf(time/365/24/60/60/1000).IntegerValue();*/
Date date = new Date();
Integer age = date.getYear()-birthday.getYear();
return age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getUserRole() {
return userRole;
}
public void setUserRole(Integer userRole) {
this.userRole = userRole;
}
public Integer getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Integer getModifyBy() {
return modifyBy;
}
public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
}
3.创建数据访问接口,UserMapper 为了结构清晰,便于浏览,接口中直写了一个方法用于演示
package cn.einblatt.ssm.dao;
import cn.einblatt.ssm.entity.Bill;
import cn.einblatt.ssm.entity.Provider;
import cn.einblatt.ssm.entity.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface UserMapper {
//查询用户数量
public int getCount();
}
接口实现类 UserMapperImpl
package cn.einblatt.ssm.dao;
import cn.einblatt.ssm.entity.Bill;
import cn.einblatt.ssm.entity.Provider;
import cn.einblatt.ssm.entity.User;
import cn.einblatt.ssm.util.CreateSqlSession;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
import java.util.Map;
public class UserMapperImpl implements UserMapper {
private SqlSessionTemplate sqlSession; //此类实现了SqlSession,功能类似于SqlSession
@Override
public int getCount() {
return sqlSession.getMapper(UserMapper.class).getCount();
}
}
4.配置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 namespace="cn.einblatt.ssm.dao.UserMapper">
<!--查询所有用户的总数-->
<select id="getCount" resultType="int">
SELECT COUNT(*) FROM `smbms_user`
</select>
</mapper>
5.配置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>
<!--映射实体类名-->
<typeAliases>
<package name="cn.einblatt.ssm.entity"></package>
</typeAliases>
</configuration>
6.配置appplicationContext.xml文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--导入配置文件-->
<!--<context:property-placeholder location="database.properties"/>-->
<!--配置数据源方式一-->
<!--<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">-->
<!--<property name="driverClassName" value="${driver}"/>-->
<!--<property name="url" value="${url}"/>-->
<!--<property name="username" value="${username}"/>-->
<!--<property name="password" value="${password}"/>-->
<!--</bean>-->
<!--配置数据源方式二-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--配置SqlSessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/>
<!--引入mybatis-config.xml配置的内容-->
<property name="configLocation" value="mybatis-conf.xml"/>
<!--扫描Mapper文件--> //这里用于演示所以只扫描了一个Mapper文件
如果有多个Mapper文件,可将它们同一包下
然后扫描整个包
<property name="mapperLocations">
<list>
<value>UserMapper.xml</value>
</list>
</property>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<bean id="userMapper" class="cn.einblatt.ssm.dao.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
7.测试
import cn.einblatt.ssm.dao.UserMapper;
import cn.einblatt.ssm.dao.UserMapperImpl;
import cn.einblatt.ssm.entity.Game.Equip;
import cn.einblatt.ssm.entity.Game.Player;
import cn.einblatt.ssm.service.GameSeivice;
import cn.einblatt.ssm.service.Impl.GameServiceImpl;
import cn.einblatt.ssm.service.Impl.UserServiceImpl;
import cn.einblatt.ssm.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import spring.Bumblebee;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
public class TestSpring {
@Test
public void getSpring(){
//方式一
ApplicationContext applicationContext= new
ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper= (UserMapper) applicationContext.getBean("userMapper");
System.out.println("用户总数:"+userMapper.getCount());
};
}
结果如下: