文章目录
一、JDBCTemplate
相关依赖
<!--spring支持jdbc模板操作-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
使用
这里以school库中student表为例进行操作
public class JDBCTemplateDemo {
public static void main(String[] args) {
//创建对象,设置数据源信息
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/school");
driverManagerDataSource.setUsername("root");
driverManagerDataSource.setPassword("123456");
//获取jdbc模板实例
JdbcTemplate jdbcTemplate = new JdbcTemplate(driverManagerDataSource);
//通过模板对象调用方法来执行相关操作
//通过 jdbcTemplate提供的 update方法来完成变更操作(修改、新增、删除) batchUpdate表示批量操作
//删除操作 SQL
String deleteSql = "delete from Student where SID = ?";
int update = jdbcTemplate.update(deleteSql, new Object[]{40});//删除 SID为 40的对象
System.out.println(update);
//查询操作(单个对象) 使用 queryForObject方法
String selectSql1 = "select * from Student where SID = ?";//?为占位符
Student1 student1 = jdbcTemplate.queryForObject(selectSql1, new Object[]{4}, new StudentMapper());
System.out.println(student1);
//查询操作(多个结果集对象) 使用 query方法
String selectSql2 = "select * from Student";
List<Student1> student1List = jdbcTemplate.query(selectSql2, new StudentMapper());
Iterator<Student1> iterator = student1List.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
JDBCTemplate解决了JDBC结果集合Java代码的耦合性问题,实现一个RowMapper接口的实现类,手动完成数据库结果集和Java对象的映射。
public class StudentMapper implements RowMapper<Student1> {
@Override
/**
* 使 jdbc模板能够完成自动映射为 Java对象关键点在于实现 RowMapper接口
* 实现 mapRow方法
* ResultSet:数据库结果集
* int:类似数据库数据序号,从0开始
* 返回类型:指定的 Java对象
* 该方法主要是完成 ResultSet结果集手动映射为 Java对象
*/
public Student1 mapRow(ResultSet resultSet, int i) throws SQLException {
Student1 student1 = new Student1();
student1.setSID(resultSet.getInt("SID"));
student1.setSage(resultSet.getInt("Sage"));
student1.setSname(resultSet.getString("Sname"));
student1.setSsex(resultSet.getString("Ssex"));
return student1;
}
}
不管是单个结果集还是多个结果集都会调用到 RowMapper接口 的实现类,数据库中每一条记录都会执行到 mapRow 方法。
二、spring 和 mybatis的整合
思路
- 在 mybatis 中,操作数据库需要获取到SQLSession对象,而该对象的实例过程在mybatis是通过 SQLSessionFactoryBuilder 读取全局配置文件来实例化一个SQLSessionFactory(会话工厂),通过SQLSessionFactory来获取SQLSession对象。
- mybatis和spring整合 过程中,可以通过 单例 的形式来管理SQLSessionFactory对象,mybatis-spring 中提供了一个 SQLSessionFactoryBean 类来实例化SQLSessionFactory,SQLSessionFactory中需要必须的属性是 DataSource 以及 Mappers。
步骤
引入mybatis-spring
为了完成spring和mybatis的整合,由mybatis提供了一个整合jar包:mybatis-spring
<!--mybatis和spring整合依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
mybatis相关开发
- mybatis全局配置文件
<?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>
<!--
数据源、mapper...
-->
</configuration>
- Pojo类(使用上面的Student1)
- Mapper.java接口文件
public interface StudentMapper {
public Student1 selectStudentById(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">
<mapper namespace="dao.StudentMapper">
<select id="selectStudentById" parameterType="int" resultType="bean.Student1">
select * from Student where SID = #{id}
</select>
</mapper>
Spring的开发
spring配置文件 spring-mybatis.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!--数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/school"/>
<property name="user" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--获取会话工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--数据源-->
<property name="dataSource" ref="dataSource"/>
<!--加载 mybatis的配置文件-->
<property name="configLocation" value="mybatis.config.xml"/>
<!--加载 mybatis的 mapper配置-->
<property name="mapperLocations" value="mapper/*.xml"/>
</bean>
<!--获取 StudentMapper的代理对象-->
<bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!--会话工厂-->
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<!--指定接口文件位置-->
<property name="mapperInterface" value="dao.StudentMapper"/>
</bean>
</beans>
使用容器管理的对象来获取数据库数据
public class MybatisDemo {
public static void main(String[] args) {
String path = "spring-mybatis.xml";
ClassPathXmlApplicationContext applicationContext =
new ClassPathXmlApplicationContext(path);
//在IOC容器获取需要的对象实例
StudentMapper studentMapper = (StudentMapper) applicationContext.getBean("studentMapper");
studentMapper.selectStudentById(4);
}
}