spring 整合mytabis
就是在spring架构中 使用mybatis的功能
等于在非spring架构时写的代码中导入的mybatis tar包,做mybatis的核心配置文件,与映射配置文件,及动态代理接口的操作。
然后是dao、service、controller三条线的代码传递数据,转换数据类型的之类的操作。
mybatis的非spring环境 步骤
1.实体类与表
2.业务层接口与实现
3.数据层接口
4.Mybatis核心配置
5.Mybatis映射配置
6.客户端程序测试功能
mybatis的spring环境 步骤
1.实体类与表
2.业务层接口与实现(提供数据层接口的注入操作)
3.数据层接口
4.Mybatis核心配置(交给spring控制,该文件省略)
5.Mybatis映射配置
6.客户端程序测试功能(使用spring方式获取bean)
7.Spring核心配置文件
8.Druid数据源的应用(可选)
9.Spring整合MyBatis
基础准备工作
1.导入Spring坐标,MyBatis坐标,MySQL坐标,Druid坐标
- 业务类与接口准备
2.创建数据库表,并制作相应的实体类Account
3.定义业务层接口与数据层接口
4.在业务层调用数据层接口,并实现业务方法的调用
- 基础配置文件
5.jdbc.properties
6.MyBatis映射配置文件
- 整合前基础准备工作
1.spring配置文件,加上context命名空间,用于加载properties文件
2.开启加载properties文件
3.配置数据源druid(备用)
4.定义service层bean,注入dao层bean
5.dao的bean无需定义,使用代理自动生成
导入Spring坐标,mybatis坐标,MySQL坐标
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<name>spring_day01_account</name>
<groupId>com.itheima</groupId>
<artifactId>spring_day01_account</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
dao接口
public interface AccountDao {
void save(Account account);
void delete(Integer id);
void update(Account account);
List<Account> findAll();
Account findById(Integer id);
}
service接口
public interface AccountService {
void save(Account account);
void delete(Integer id);
void update(Account account);
List<Account> findAll();
Account findById(Integer id);
}
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_db
jdbc.username=root
jdbc.password=itheima
mybatis核心配置文件 SqlMapConfig.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>
<properties resource="jdbc.properties"></properties>
<typeAliases>
<package name="com.itheima.domain"/>
</typeAliases>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.itheima.dao"></package>
</mappers>
</configuration>
spring配置文件 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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
AccountDao.xml配置文件 也就是mybatis的映射配置文件
用来配置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="com.itheima.dao.AccountDao">
<!--配置根据id查询-->
<select id="findById" resultType="account" parameterType="int">
select * from account where id = #{id}
</select>
<!--配置查询所有-->
<select id="findAll" resultType="account">
select * from account
</select>
<!--配置保存-->
<insert id="save" parameterType="account">
insert into account(name,money)values(#{name},#{money})
</insert>
<!--配置删除-->
<delete id="delete" parameterType="int">
delete from account where id = #{id}
</delete>
<!--配置根据名称查询-->
<select id="findByName" resultType="account" parameterType="string">
select * from account where name = #{name}
</select>
<!--配置更新-->
<update id="update" parameterType="account">
update account set name=#{name},money=#{money} where id=#{id}
</update>
</mapper>
接下来在pom文件中
加入spring、druid的坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
这是mybatis整合spring的tar包
有了它spring才能知道起mybatis进来了,并控制mybatis的功能
但这个也是属于mybatis的tar
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
创建service层实现类
public class AccountServicetImpl implements AccountService {
//注入dao层接口
private AccountDao accountDao;
//用set方式注入依赖 需要提供dao层的set方法
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public void delete(Integer id) {
accountDao.delete(id);
}
public void update(Account account) {
accountDao.update(account);
}
public List<Account> findAll() {
return accountDao.findAll();
}
public Account findById(Integer id) {
return accountDao.findById(id);
}
}
spring配置文件 applicationContext.xml
<!--配置service作为spring的bean,注入dao-->
<bean id="accountService" class="com.itheima.service.impl.AccountServicetImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
因为已经在AccountServicetImpl写了注入dao层接口
因此不用在对dao声明注入bean中,ref属性会自动进行声明 注入bean中
但现在 还没有加相关的配置,要做了相关配置才行,
现在运行它是要报错的
spring配置文件 applicationContext.xml
在它空间名中增加 context 空间名称 与 http路径
xmlns:context="http://www.springframework.org/schema/context"
注意:这两行会报错,出现红色下划线
这是由于引号位置写错,这几个路径都要写在一个引号中
是从xsi:schemaLocation= 的后面开始的
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd"
<!--加载perperties配置文件的信息-->
<context:property-placeholder location="classpath:*.properties"/>
"classpath:*.properties 这个作用是加载所有后缀名是properties的配置文件
再将 jdbc.properties文件中的信息加入到spring配置文件中
这就是用druid的功能创建数据库链接,建立数据库连接池
注意value属性中的格式 ${},不记笔记就老是写错的地方
里面写的是jdbc.properties key的名称
<!--加载druid资源 也就是数据库的资源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
这是将mybatis核心文件中的 别名 与映射的dao层dao接口的路径
写到spring配置文件中
要使用的对象、包名从坐标的依赖路径里找到
<!--spring整合mybatis后控制的创建连接用的对象-->
class 写的是依赖mybatis-spring中的SqlSessionFactoryBean资源路径
SqlSessionFactoryBean用来对mybatis创建bean的
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
通过ref属性来找到dataSource参照的对象 作用:创建数据资源连接对象
<property name="dataSource" ref="dataSource"/>
typeAliasesPackage mybatis核心文件中的 别名标签+包标签 value 别名的包路径
<property name="typeAliasesPackage" value="com.itheima.domain"/>
</bean>
就是mybatis核心文件中的这两句代码
<typeAliases>
<package name="com.itheima.domain"/>
</typeAliases>
这一步才是实现mybatis作为spring的bean进行管理的配置
有了它 dao层与mybatis映射文件里的SQL就能建立起关联了
<!--加载mybatis映射配置的扫描,将其作为spring的bean进行管理-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.dao"/>
</bean>
class=“org.mybatis.spring.mapper.MapperScannerConfigurer”
这个class写的是 依赖坐标中映射配置扫描器的资源路径
就是等于这句话
<mappers>
<package name="com.itheima.dao"></package>
</mappers>
加载mybatis映射配置的扫描 与 spring整合mybatis后控制
这两个的 对象都可以 不写 id的
原因是 做自动代理创建dao用的,将它交给spring配置文件中
spring就能用,是给spring自己用的,spring只需要看到这个类型就可以了
现在如果测试时出现报错,的去看看它pom文件中有没有写jdbc的坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
写完这几个配置后就可以不要mybatis核心文件了
易错点 version 的版本要与 spring-context的版本一致
不然会报错,很难想得到的
mybatis映射配置文件还是要的
为了后面维护工作方便,都将id 写上去