Spring--jdbcTemplate
Posted Jason–json
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring--jdbcTemplate相关的知识,希望对你有一定的参考价值。
概述:jdbcTemplat对java的jdbc进行了简单封装,用来操作关系型数据库。
作用:用于与数据库交互,实现对数据库crud的操作。
代码实现:
环境搭建--使用的时mysql数据库,IDEA工具,创建MAVEN工程。
1.需要导入的依赖包:
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
2.对应数据库user表的实体类
public class User {
private Integer id;
private String uname;
private String pwd;
//避免代码看起来繁琐,没有贴入get/set/toString方法
}
3.jdbcTemplate基本使用
public class JDBCTemplateDemo {
public static void main(String[] args) {
//1.准备数据源,spring内部提供给
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://127.0.0.1:3306/template");
ds.setUsername("root");
ds.setPassword("root");
//2.创建JDBDTemplate对象
JdbcTemplate jt = new JdbcTemplate();
//3.给jt设置数据源
jt.setDataSource(ds);
//4.执行操作
jt.execute("INSERT INTO user(uname,pwd) VALUES(‘赏金‘,‘abc‘)");
}
}
4.dao实现jdbcTemplate----dao接口层
import com.pojo.User; import java.util.List; public interface UserDao { //查询所有的User数据 List<User> findAll(); //根据ID查询User User getUserById(Integer id); //修改User void updateUser(User u); }
4.dao实现jdbcTemplate----dao接口实现类
import com.dao.UserDao; import com.pojo.User; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import java.util.List; public class UserDaoImpl implements UserDao { private JdbcTemplate jdbcTemplate; //BeanPropertyRowMapper<>类,用于将数据库查询结果转换为Java类对象 private BeanPropertyRowMapper<User> bprm= new BeanPropertyRowMapper<User>(User.class); public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } //查询所有User public List<User> findAll() { String sql="select * from user"; List<User> userList = jdbcTemplate.query(sql, bprm); return userList; } //根据Id查询User public User getUserById(Integer id) { String sql="select * from user where id=?"; List<User> query = jdbcTemplate.query(sql, bprm, id); return query.get(0); } //更新User public void updateUser(User u) { String sql="update user set uname=?,pwd=? where id=?"; jdbcTemplate.update(sql,u.getUname(),u.getPwd(),u.getId()); } }
5.配置properties和xml文件
#数据源的jdbc.properties配置文件
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/template
jdbc.username=root
jdbc.password=root
<?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.xsd">
<!--引入jdbc文件-->
<bean id="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:/jdbc.properties</value>
</property>
</bean>
<!--配置账户持久层-->
<bean id="userDaoImpl" class="com.dao.impl.UserDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<!-- 给jdbcTemplate引入数据源-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
6.测试
import com.dao.impl.UserDaoImpl;
import com.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class JDBCtemplateDemo2 {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("application.xml");
UserDaoImpl userDao =(UserDaoImpl)ac.getBean("userDaoImpl");
//查询所有测试
List<User> all = userDao.findAll();
for (User user : all) {
System.out.println(user);
}
//根据ID查测试
User user = userDao.getUserById(1);
System.out.println(user);
//修改User测试
User user1 = new User();
user1.setId(3);
user1.setUname("赵信");
user1.setPwd("456");
userDao.updateUser(user1);
}
}
//得到的测试结果,均成功实现
优化代码
1.将JdbcTemplate和BeanPropertyRowMapper<>抽取出来做父类,
import com.pojo.User; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; public class JdbcTemplateBase { private JdbcTemplate jdbcTemplate; //BeanPropertyRowMapper<>类,用于将数据库查询结果转换为Java类对象 private BeanPropertyRowMapper<User> bprm= new BeanPropertyRowMapper<User>(User.class); public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } public BeanPropertyRowMapper<User> getBprm() { return bprm; } public void setBprm(BeanPropertyRowMapper<User> bprm) { this.bprm = bprm; } }
2.继承父类的实现类
import com.dao.JdbcTemplateBase; import com.dao.UserDao; import com.pojo.User; import java.util.List; public class UserDaoImplEx extends JdbcTemplateBase implements UserDao { //查询所有User public List<User> findAll() { String sql="select * from user"; List<User> userList = getJdbcTemplate().query(sql, getBprm()); return userList; } //根据Id查询User public User getUserById(Integer id) { String sql="select * from user where id=?"; List<User> query = getJdbcTemplate().query(sql, super.getBprm(), id); return query.get(0); } //更新User public void updateUser(User u) { String sql="update user set uname=?,pwd=? where id=?"; getJdbcTemplate().update(sql,u.getUname(),u.getPwd(),u.getId()); } }
3.修改.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.xsd"> <!--引入jdbc文件--> <bean id="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:/jdbc.properties</value> </property> </bean> <!--配置账户持久层--> <bean id="userDaoImpl" class="com.dao.impl.UserDaoImpl"> <property name="jdbcTemplate" ref="jdbcTemplate"/> </bean> <bean id="userDaoImplEx" class="com.dao.impl.UserDaoImplEx"/> <bean id="jdbcTemplateBase" class="com.dao.JdbcTemplateBase"> <property name="jdbcTemplate" ref="jdbcTemplate"/> </bean> <!-- 给jdbcTemplate引入数据源--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <!--配置数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> </beans>
以上是关于Spring--jdbcTemplate的主要内容,如果未能解决你的问题,请参考以下文章