@[TOC]
前言
看完【JAVA】Srping和传统JDBC实现数据库操作之后,是否觉得传统的JDBC太繁琐了,就算是只写一个简单的数据库插入功能都要写好多与业务无关的代码,那么使用spring封装的JdbcTemplate
就很有必要了,当然JdbcTemplate也是直连的数据源。传统JDBC和JdbcTemplate区别
创建数据库
首先创建我们的数据库(这里我使用的是Mysql),为了演示方便,我这里简单的创建一个spring数据库,然后数据库有一个user用户表:
- 创建一个名为
spring
的数据库。 - 创建一个名为user的数据表,表包括id、email、name、password四个字段。
CREATE TABLE `user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;
复制代码
创建实体类
创建一个实体类和数据库的表相对应(模型用来储存要操作的数据)。
package cn.biecheng.www.Entity;
public class User {
int id;
String name;
String email;
String password;
public User(String name, String email, String password){
this.email = email;
this.name = name;
this.password = password;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public String getEmail() {
return email;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public void setEmail(String email) {
this.email = email;
}
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
}
复制代码
id
、
email
、
name
、
password
分别对应数据表user的
字段,为每个成员属性添加
getter
和
setter
方法,实现对成员属性的操作。
数据访问对象(DAO)模式
DAO(data access object),数据库访问对象,主要的功能就是用于惊险数据库操作的。
UserDao接口
package cn.biecheng.www.Dao;
import cn.biecheng.www.Entity.User;
public interface UserDao {
public void inSert(User user);
}
复制代码
UserDao接口的实现
package cn.biecheng.www.Dao.impl;
import cn.biecheng.www.Dao.UserDao;
import cn.biecheng.www.Entity.User;
import org.springframework.jdbc.core.JdbcTemplate;
public class UserDaoImpl implements UserDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
this.jdbcTemplate = jdbcTemplate;
}
public void inSert(User user) {
jdbcTemplate.update("insert into user(name,email,password) values(?,?,?)", user.getName(), user.getEmail(), user.getPassword());
}
}
复制代码
jdbcTemplate.update("insert into user(name,email,password) values(?,?,?)", user.getName(), user.getEmail(), user.getPassword());
复制代码
一行代码即可实现插入功能,和传统的JDBC相比实现插入真的是简单到不行吧!
数据源配置
<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-2.5.xsd">
<!--装配UserDaoImpl类-->
<bean id="userDaoImple" class="cn.biecheng.www.Dao.impl.UserDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!--配置jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--建立连接-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="url" value="jdbc:mysql://localhost:3306/spring"></property>
</bean>
</beans>
复制代码
- 通过
org.springframework.jdbc.datasource.DriverManagerDataSource
建立数据源连接(连接对象Connection) - 配置JdbcTemplate,其中
ref="dataSource"
的dataSource为数据源连接的id - 装配装配
UserDaoImpl
类,将JdbcTemplate对象注入到UserDaoImpl类的名为jdbcTemplate成员属性中
装配Bean
package cn.biecheng.www.Config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration;
@Configuration
@ImportResource(locations = {"DataSource.xml"})
@ComponentScan(basePackages = {"cn.biecheng.www"})
public class Config {
}
复制代码
@Configuration
声明这个是配置类,@ImportResource
装配xml配置文件(Spring-Datasource.xml为直连数据源的配置文件),@ComponentScan
开启组件扫描。
测试类
import cn.biecheng.www.Dao.UserDao;
import cn.biecheng.www.Entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {cn.biecheng.www.Config.Config.class})
public class test {
@Autowired
UserDao userDao;
@Test
public void test(){
User user;
user = new User("xue8", "xue8", "xue8");
userDao.inSert(user);
}
}
复制代码
运行测试类。 最后我们数据库成功插入了我们插入的数据。
欢迎加入JAVA学习群949419296,一起交流!