Spring+Mybais整合
简单的来说,Spring主要用于在业务层(当然spring也有数据库交互的模块,个人觉得spring在这方面有一点不如mybatis),而mybatis主要用于数据持久化,在一个完整的项目中无论是业务代码,还是与数据库交互部分的代码缺一不可,下边我来说一下spring和mybatis整合方法。
1.导入依赖:(spring核心依赖包这里就不写了)
1 <dependency> 2 <groupId>org.mybatis</groupId> 3 <artifactId>mybatis-spring</artifactId> 4 <version>1.3.2</version> 5 </dependency> 6 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> 7 <dependency> 8 <groupId>org.mybatis</groupId> 9 <artifactId>mybatis</artifactId> 10 <version>3.4.1</version> 11 </dependency> 12 13 <!--spring与Mybatis整合包-->
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.32</version> </dependency> <!-- https://mvnrepository.com/artifact/c3p0/c3p0 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!--jdbc依赖和数据源,这里我是用的是c3p0提供的,还有dbcp,和spring自带的-->
1 <dependency> 2 <groupId>org.springframework</groupId> 3 <artifactId>spring-tx</artifactId> 4 <version>5.1.0.RELEASE</version> 5 </dependency> 6 <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> 7 <dependency> 8 <groupId>org.springframework</groupId> 9 <artifactId>spring-jdbc</artifactId> 10 <version>5.1.0.RELEASE</version> 11 </dependency> 12 <!--托管事务的依赖-->
2.创建数据表
3.编写java代码
实体类
1 public class Account { 2 private int accountid; 3 private String accountname; 4 private Double accountmonkey; 5 //省略set、get方法 6 }
DAO层接口
//DAO层接口,不写实现类。 public interface AccountDao { List<Account>getAll();//查询数据库中所有信息 }
Service层接口
1 public interface AccountService { 2 List<Account> getAll();//查询所有 3 }
Service层实现类
1 public class AccountServiceImpl implements AccountService { 2 //注入dao接口实例,省略set、get方法 3 private AccountDao dao; 4 @Override 5 public List<Account> getAll() { 6 return dao.getAll(); 7 } 8 }
4.数据库连接参数(database.properties)
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3307/account jdbc.username=root jdbc.password=root
5.核心配置文件(applicationContext.xml)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 7 <!--引入外部配置文件,这里引入的是保存数据库连接参数的database.properties文件--> 8 <context:property-placeholder location="classpath:database.properties"/> 9 10 <!--引入c3p0数据源--> 11 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 12 <property name="driverClass" value="${jdbc.driver}"/> 13 <property name="jdbcUrl" value="${jdbc.url}"/> 14 <property name="user" value="${jdbc.username}"/> 15 <property name="password" value="${jdbc.password}"/> 16 </bean> 17 <!--Mybatis核心配置--> 18 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 19 <!--配置数据源--> 20 <property name="dataSource" ref="dataSource"/> 21 <!--小配置文件的目录--> 22 <property name="mapperLocations" value="classpath:mapper/*.xml"/> 23 <!--实体类别名--> 24 <property name="typeAliasesPackage" value="com.cn.entity"/> 25 </bean> 26 <!--mapper代理对象,生成dao动态代理,也就是说每多一个DAO接口,就要创建一个这样的节点--> 27 <bean id="accountDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> 28 <!--将sqlSessionFactory注入--> 29 <property name="sqlSessionFactory" ref="sqlSessionFactory"/> 30 <!--接口注入--> 31 <property name="mapperInterface" value="com.cn.dao.AccountDao"/> 32 </bean> 33 <!--service层bean实例--> 34 <bean id="service" class="com.cn.service.impl.AccountServiceImpl"> 35 <property name="dao" ref="accountDao"></property> 36 </bean> 37 <!--mapper文件扫描,看情况使用(我没用过,应该是在sqlSessionFactory中没有做配置时可以这样配置)--> 38 <!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 39 <property name="basePackage" value="/com.cn.dao"/> 40 </bean>--> 41 <!--开启事务,目前没啥用,就是个习惯--> 42 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 43 <property name="dataSource" ref="dataSource"/> 44 </bean> 45 46 </beans>
哦还有一个小配置文件
<?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.cn.dao.accountDao"> <select id="getAll" resultType="Account"> select * from account </select> </mapper>
大功告成,开始测试
1 public class App 2 { 3 public static void main( String[] args ) 4 { 5 ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext.xml"); 6 AccountService bean = context.getBean(AccountService.class); 7 System.out.println(bean.getAll()); 8 } 9 }
下期使用存java配置方式进行配置