前言
先入门,把核心理解后再回过头来看这些我觉得效果更好,不知道说的对不对,要是理解不当,还望指教。此篇为Spring的数据库编程,主要讲jdbcTemplate,让初学者直接上手。
数据库切入
数据库编程是互联网编程的基础,Spring框架为开发者提供了JDBC模板模式,即jdbcTemplate,它可以简化许多代码,需要提醒的是jdbcTemplate在实际应用中并不常用,但我觉得对新手来说苍蝇再小也是肉,要想往前走,就得把这些小肉先吃了。
Spring JDBC配置
使用Spring JDBC操作数据库,需要对其进行配置,配置如下
- <!--配置spring-jdbcTemplate-->
- <!--配置数据源-->
- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <!--MySQL数据库驱动-->
- <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
- <!--连接数据库的URL-->
- <property name="url" value="jdbc:mysql://localhost:3306/bbb?useUnicode=true&characterEncoding=UTF-8"></property>
- <!--连接数据库的用户名-->
- <property name="username" value="root"></property>
- <!--连接数据库的密码-->
- <property name="password" value="root"></property>
- </bean>
- <!--配置JDBC模板-->
- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
- <property name="dataSource" ref="dataSource"></property>
- </bean>
配置JDBC模板时需要将dataSource注入到jdbcTemplate,而在数据访问层(Dao类)需要使用jdbcTemplate时也需要将jdbc Template注入到对应的Bean中,我在这演示用最简单的注解注入
- @Repository("userDao")
- public class UserDaoImpl implements UserDao {
- @Autowired
- //使用配置文件中的JDBC模板
- private JdbcTemplate jdbcTemplate;
- }
Spring Jdbc Template的常用方法
在上面我们获取了JDBC模板,下面我来说如何使用它。首先需要了解JdbcTemplate常用的方法,该类的常用方法是update和query.
1、public int update(String sql,Object args[])
该方法可以对数据表进行增加、修改、删除等操作。使用args[]设置SQL语句中的参数,并返回更新的行数。
示例如下:
- public void add() {
- String insertSql = "insert into user values(null,?,?)";
- Object parem1[] = {"成功","123456"};
- jdbcTemplate.update(insertSql,parem1);
- System.out.println("UserDao中的添加功能实现了");
- }
2、public List
该方法可以对数据表进行查询操作,rowMapper将结果集映射到用户自定义的类中(前提是自定义类中的属性要与数据表的字段对应)。
示例如下:
- public void query() {
- String selectSql = "select * from user";
- RowMapper<User> rowMapper = new BeanPropertyRowMapper<User>(User.class);
- List<User> list = jdbcTemplate.query(selectSql,rowMapper,null);
- System.out.println("UserDao中的查询功能实现了");
- }
案例辅助
Pom.xml
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.11</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
- <version>3.1.0</version>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jstl</artifactId>
- <version>1.2</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.38</version>
- </dependency>
- <!--spring核心依赖-->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>5.1.5.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-beans</artifactId>
- <version>5.1.5.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>5.1.5.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aop</artifactId>
- <version>5.1.5.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>5.1.5.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>5.1.5.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>5.1.5.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-expression</artifactId>
- <version>5.1.5.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <version>5.1.5.RELEASE</version>
- </dependency>
- <!--日志相关-->
- <dependency>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- <version>1.2</version>
- </dependency>
- </dependencies>
spring-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:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- 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/mvc
- http://www.springframework.org/schema/mvc/spring-mvc.xsd
- ">
- <!--配置注解要扫描的包-->
- <context:component-scan base-package="com.my"></context:component-scan>
- <mvc:annotation-driven></mvc:annotation-driven>
- <!--将指定类TestDaoImpl配置给Spring,让Spring创建其实例-->
- <bean id="testDIDao" class="com.my.dao.impl.TestDaoImpl"/>
- <!--使用构造方法注入-->
- <!--<bean id="testDIService" class="com.my.service.impl.TestServiceImpl" >-->
- <!--<!–将TestDIDao注入到TestDIServiceImpl类的属性testDao上–>-->
- <!--<constructor-arg index="0" ref="testDIDao"/>-->
- <!--</bean>-->
- <!--使用setter方法注入-->
- <bean id="testDIService" class="com.my.service.impl.TestServiceImpl">
- <!--调用TestDIServiceImpl类的setter方法,将TestDao注入到TestServiceImpl类的属性testDao上-->
- <property name="testDao" ref="testDIDao"></property>
- </bean>
- <!--配置spring-jdbcTemplate-->
- <!--配置数据源-->
- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <!--MySQL数据库驱动-->
- <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
- <!--连接数据库的URL-->
- <property name="url" value="jdbc:mysql://localhost:3306/bbb?useUnicode=true&characterEncoding=UTF-8"></property>
- <!--连接数据库的用户名-->
- <property name="username" value="root"></property>
- <!--连接数据库的密码-->
- <property name="password" value="root"></property>
- </bean>
- <!--配置JDBC模板-->
- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
- <property name="dataSource" ref="dataSource"></property>
- </bean>
- <!--配置事务-->
- <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"></property>
- </bean>
- <!--注册事务注解驱动-->
- <tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven>
- </beans>
User
- package com.my.pojo;
- public class User {
- private int id;
- private String username;
- private String password;
- public User() {
- }
- public User(int id, String username, String password) {
- this.id = id;
- this.username = username;
- this.password = password;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- @Override
- public String toString() {
- return "User{" +
- "id=" + id +
- ", username='" + username + ''' +
- ", password='" + password + ''' +
- '}';
- }
- }
UserDao
- package com.my.dao;
- public interface UserDao {
- public void add();
- public void delete();
- public void update();
- public void query();
- }
UserDaoImpl
- @Override
- public void update() {
- String updateSql = "update user set username=? , password=? where id =?";
- Object parem3[] = {"修改","654321",3};
- jdbcTemplate.update(updateSql,parem3);
- System.out.println("UserDao中的修改功能实现了");
- }
- @Override
- public void query() {
- String selectSql = "select * from user";
- RowMapper<User> rowMapper = new BeanPropertyRowMapper<User>(User.class);
- List<User> list = jdbcTemplate.query(selectSql,rowMapper,null);
- System.out.println("UserDao中的查询功能实现了");
- for (User user : list){
- System.out.println(user);
- }
- }
- }
测试Test3
- package com.my.test;
- import com.my.dao.UserDao;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Test3 {
- @Autowired
- private UserDao userDao;
- public static void main(String[] args) {
- ApplicationContext appCon = new ClassPathXmlApplicationContext("spring-config.xml");
- //从容器中获取目标对象
- UserDao userDao = (UserDao) appCon.getBean("userDao");
- //userDao.add();
- userDao.delete();
- userDao.update();
- userDao.query();
- }
- }
测试结果
结束
对于Spring JbdcTemplate的讲解到此结束,对于新手来说没必要太复杂,能完成增删改查做个入门即可,等了解了再详细学习才有帮助,希望能对一些需要的朋友有用吧。