前言
好久没有用过ssm了,有点陌生,最近回顾一下,文章的所有资源都来自《Java EE 企业级应用开发教程》这本书
一、环境搭建准备
1.导入相应的jar包
2.编写配置文件
src根目录下创建db.properties文件、spring的配置文件以及mybatis的配置文件
db.properties
# db.properties文件配置
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://:3306/bybaitis?serverTimezone=UTC
jdbc.username=root
jdbc.password=123456
#新添加
jdbc.maxTotal = 30
jdbc.maxIdle = 10
jdbc.initialSize = 5
#数据库连接最大数
applicationContext.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!--读取db.properties -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp2.BasicDataSource">
<!--数据库驱动 -->
<property name="driverClassName" value="${jdbc.driver}" />
<!--连接数据库的url -->
<property name="url" value="${jdbc.url}" />
<!--连接数据库的用户名 -->
<property name="username" value="${jdbc.username}" />
<!--连接数据库的密码 -->
<property name="password" value="${jdbc.password}" />
<!--最大连接数 -->
<property name="maxTotal" value="${jdbc.maxTotal}" />
<!--最大空闲连接 -->
<property name="maxIdle" value="${jdbc.maxIdle}" />
<!--初始化连接数 -->
<property name="initialSize" value="${jdbc.initialSize}" />
</bean>
<!-- 事务管理器,依赖于数据源 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 配置MaBatis工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源-->
<property name="dataSource" ref="dataSource"/>
<!-- 指定核心配置文件位置-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 实例化dao -->
<bean id="customerDao" class="dao.impl.CustomerDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- Mapper代理开发(基于MapperFactoryBean)-->
<bean id="customerMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="mapper.CustomerMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- Mapper代理开发(基于MapperScannerConfigurer) 可代替基于MapperFactoryBean的spring配置和mybatis文件的引入 -->
<!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> -->
<!-- <property name="basePackage" value="mapper" />-->
<!-- </bean>-->
<!-- 开启扫描 -->
<context:component-scan base-package="service"></context:component-scan>
</beans>
mybatis-config.xml
<?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="po"/>
</typeAliases>
<mappers>
<mapper resource="po/CustomerMapper.xml"/>
<mapper resource="mapper/CustomerMapper.xml"/>
</mappers>
</configuration>
二、传统dao方式
1.在src目录下创建po包,创建持久类Customer
package po;
/*
* 持久化类
*/
public class Customer {
private Integer id;
private String username;
private String jobs;
private String phone;
String pre = "abc/img/classify";
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = this.pre + username;
}
public String getJobs() {
return jobs;
}
public void setJobs(String jobs) {
this.jobs = jobs;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Customer [id=" + id + ", username=" + username + ", jobs=" + jobs + ", phone=" + phone + "]";
}
}
在po包下创建映射文件CustomerMapper.xml,编写根据id查询客户的映射语句
<?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="po.CustomerMapper">
<select id="findCustomerById" parameterType="Integer" resultType="customer">
select * from customer where id = #{id}
</select>
</mapper>
在mybatis-config.xml中配置CustomerMapper.xml文件的位置
<mappers>
<mapper resource="po/CustomerMapper.xml"/>
<mapper resource="mapper/CustomerMapper.xml"/>
</mappers>
2.实现dao层
在src目录下创建dao包,创建CustomerDao接口,创建一个根据id查询客户的接口方法
package dao;
import po.Customer;
public interface CustomerDao {
Customer findCustomerById(Integer id);
}
在src目录下创建dao.impl包,创建CustomerDao接口的实现类CustomerDaoImpl
package dao.impl;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import dao.CustomerDao;
import po.Customer;
public class CustomerDaoImpl extends SqlSessionDaoSupport implements CustomerDao {
@Override
public Customer findCustomerById(Integer id) {
return this.getSqlSession().selectOne("po.CustomerMapper.findCustomerById",id);
}
}
在applicationContext.xml中编写CUstomerDaoImpl的配置
<!-- 实例化dao -->
<bean id="customerDao" class="dao.impl.CustomerDaoImpl">
<!--注入SqlSessionFactory对象实例-->
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
3.测试
创建test包,创建DaoTest类,编写测试方法
package test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import dao.CustomerDao;
import mapper.CustomerMapper;
import po.Customer;
/*
* Dao测试类
*/
public class DaoTest {
@Test
public void findCustomerByIdDaoTest() {
/*
* dao方法测试
*/
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
// CustomerDao customerDao = (CustomerDao) act.getBean("customerDao");
CustomerDao customerDao = act.getBean(CustomerDao.class);
Customer customer = customerDao.findCustomerById(1);
System.out.println(customer);
}
}
三、Mapper接口方式
这个方法要求接口文件和映射文件放在同一个包,且mapper接口的名称和对应的mapper.xml映射文件的名称一致等
1.在src目录下,创建mapper包,创建CustomerMapper相应的接口文件和映射文件
package mapper;
import po.Customer;
public interface CustomerMapper {
Customer findCustomerById(Integer id);
//添加客户
void addCustomer(Customer customer);
}
<?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="mapper.CustomerMapper">
<select id="findCustomerById" parameterType="Integer" resultType="customer">
select * from customer where id = #{id}
</select>
<!-- 添加客户信息 -->
<insert id="addCustomer" parameterType="customer">
insert into customer(username,jobs,phone) values (#{username},#{jobs},#{phone})
</insert>
</mapper>
2.在mybatis配置文件引入新的映射文件
<mapper resource="mapper/CustomerMapper.xml"/>
3.在spring配置文件创建customerMapper的Bean
<!-- Mapper代理开发(基于MapperFactoryBean)-->
<bean id="customerMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="mapper.CustomerMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
即通过spring容器获取CustomerMapper的实例
4.测试
@Test
public void findCustomerByIdMapperTest() {
/*
* 接口方法测试
*/
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerMapper customerMapper = act.getBean(CustomerMapper.class);
Customer customer = customerMapper.findCustomerById(1);
System.out.println(customer);
}
四、基于MapperScannerConfigurer的整合
在spring配置文件中编写关于MapperScannerConfigurer的代码
<!-- Mapper代理开发(基于MapperScannerConfigurer) 可代替基于MapperFactoryBean的spring配置和mybatis文件的引入 -->
<!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> -->
<!-- <property name="basePackage" value="mapper" />-->
<!-- </bean>-->
basePackage指定映射文件所在的包路径。指定包路径时,会扫描该包及其子包的所有文件
使用这个方法的话就不用去mybatis配置文件引入新的映射文件和在spring配置文件创建customerMapper的Bean。