为什么要整合Spring
我的想法:Spring可以帮我们管理对象,方便我们开发
整合步骤
创建项目,引入jar包
创建过程略,引入jar包如下图所示:
基本配置
- 创建User.class类
public class User {
private int id;
private String name;
private int age;
//省略get set toString方法
}
- 引入jdbc.properties文件,编写数据库连接信息
- 备注:需要是用jdbc.driver,避免出现org.springframework.jdbc.CannotGetJdbcConnectionException异常
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=111111
- 引入日志打印配置文件 log4j.properties文件
创建Mapper文件夹,配置编写Mapper.java和Mapper.xml
Mapper.java
public interface UserMapper {
/**
* 查找用户
* */
public User findUserById(Integer id);
}
Mapper.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">
<!-- namespace要写到具体文件,这个地方跟全路径扫描不一样 -->
<mapper namespace="com.syw.mapper.UserMapper">
<select id="findUserById" parameterType="Integer" resultType="User">
select * from user2 where id = #{v}
</select>
</mapper>
编写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>
<!-- 别名 -->
<typeAliases>
<package name="com.syw.bean" />
</typeAliases>
<!-- 配置mapper文件路径 -->
<mappers>
<package name="com.syw.mapper" />
</mappers>
</configuration>
编写applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 引入数据库连接文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!-- el表达式前必须加jdbc,否则会出现org.springframework.jdbc.CannotGetJdbcConnectionException -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!-- 实例化sqlSession的工厂 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:sqlMapConfig.xml" />
</bean>
<!-- Mapper动态代理开发 自动扫描basePackage下的所有文件,所以不用配置id -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 如果配置了动态代理,sqlMapConfig.xml就可以不用指定mapper的路径了,
也就是不需要设置<mappers></mappers>了 -->
<!-- 基本包 -->
<property name="basePackage" value="com.syw.mapper" />
</bean>
</beans>
编写测试类
public class TestSpringMybatis {
private ClassPathXmlApplicationContext ac;
@Before
public void setUp() throws Exception {
this.ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
}
/**
* 查询单个
*/
@Test
public void selectOneMybatis() throws Exception {
// 可以直接通过类名获取对应的mapper
UserMapper mapper = ac.getBean(UserMapper.class);
User user = mapper.findUserById(1);
System.out.print(user);
}
}