一、项目结构
之前是mybatis连接数据库进行操作。整合后,由spring中的bean来进行数据库连接操作,mybatis配置文件写mapper映射。
二、项目整合步骤
1.创建maven,导入依赖
2.两个全局配置文件(spring+mybatis)、db.properties、log4j.properties
(mybatis配置文件记得映射mapper.xml,spring配置文件操作数据库连接)
3.创建pojo,mapper-2,test.
三、代码演示
1.pom.xml
<dependencies>
<!--数据库的连接驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.8</version>
</dependency>
<!--mybatis的核心-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!--日志管理:mybatis默认使用这个日志进行管理-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--loombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--在这个位置导入你的spring相关的包-->
<!--导入spring的依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<!-- 加入spring和mybatis的联系包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!--Spring的事务-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<!--加入Spring的JDBC,就是jdbcTemplate-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<!--不用mybatis的数据连接,用c3p0-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
</dependencies>
2.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>
<!--数据库啥的不在mybatis里面弄,因为和spring整合,交给spring的bean来处理,这里面就写mapper映射就行
datasources交给spring进行注入; 属性文件也是在spring里边做即可-->
<!--加入映射-->
<mappers>
<mapper resource="mapper/StudentMapper.xml"/>
</mappers>
</configuration>
3.bean-config.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!--组件扫描器,扫描包-->
<context:component-scan base-package="com.yty.pojo"></context:component-scan>
<!--加载配置文件,也就是数据库属性文件; xxx.properties-->
<context:property-placeholder location="db.properties"></context:property-placeholder>
<!--数据库连接池-->
<bean id="dataSource1" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!--初始连接池大小-->
<property name="initialPoolSize" value="10"/>
<!--连接池中连接最小(大)个数-->
<property name="minPoolSize" value="5"/>
<property name="maxPoolSize" value="20"/>
</bean>
<!--mybatis要连接数据源,将数据源传给 sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--加载数据源-->
<property name="dataSource" ref="dataSource1"></property>
<!--加载映射-->
<property name="mapperLocations" value="mapper/StudentMapper.xml"></property>
</bean>
<!--配置StudentMapper对象(传递接口进来)-->
<bean id="StudentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!--传递工厂-->
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<property name="mapperInterface" value="com.yty.mapper.StudentMapper"/>
</bean>
</beans>
4.StudentMapper
public interface StudentMapper {
//查询所有
List<Student> queryAll();
}
5.StudnetMapper.xml
<?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.yty.mapper.StudentMapper">
<!--查询所有-->
<select id="queryAll" resultType="com.yty.pojo.Student">
select * from student;
</select>
</mapper>
6.test.java
测试记得用Spring来启动,所以是applicationContext。
mybatis启动就会是sqlsessionFactory。
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean-config.xml");
//获取bean接口
StudentMapper studentMapper = applicationContext.getBean(StudentMapper.class);
List<Student> list = studentMapper.queryAll();
for (Student student : list) {
System.out.println(student);
}
}
}
7.结果:
8.注意:
如果在你的web项目中; 直接可以配置在web.xml文件;tomcat容器启动时候就会启动spring; 就会注入我们需要的bean;