一、建立demo所需的数据库
建立数据库:day18
create database day18;
建立数据表:t_user并创建三个字段,id、username、password。
create table t_user(id int primary_key auto_increment,username varchar(50),password varchar(32));
插入几条测试数据:
insert into t_user values(null,'jack','520');
insert into t_user values(null,'rose','1314');
insert into t_user values(null,'cyh','test');
二、导入demo所需的jar包
所需jar包链接:链接: https://pan.baidu.com/s/15d2Uktc5uMJarB0xNhbftA 提取码: sxke
三、创建JavaBean、数据模型
创建User
public class User {
private Integer id; //用户id
private String username; //用户名
private String password; //用户密码
}
(1)、使用API来插入数据:
新建一个测试类,导入junit4 jar包,写入方法
public class demo1 {
@Test
public void test1(){
//1.创建数据源
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/day18");
dataSource.setUsername("root");
dataSource.setPassword("123456");
//2.创建模板
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = "insert into t_user values(null,'test','test')";
jdbcTemplate.update(sql);
}
}
运行看下结果:
说明此时我们的数据库连接配置是正确的,并且已经插入了一条测试数据。
(2)、使用beans.xml文件配置数据库插入数据
使用beans.xml文件配置数据库又分为两种形式,第一种为dbcp连接池,第二种为c3p0连接池,,下面我们对这两种名方法一一实现出来。
第一种:dbcp连接池配置数据库
新建beans.xml文件,在里面写入下列配置
<?xml version="1.0" encoding="UTF-8"?>
<!--xmlns xml namespace:xml命名空间-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p ="http://www.springframework.org/schema/p"
xmlns:context ="http://www.springframework.org/schema/context"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/day18"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="userDao" class="com.xianzhou.dao.Impl.IUserDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
</beans>
此时我们需要对User模型类增加get和set代码,因为IUserDaoImpl实现类需要调用get方法获取参数值,测试类需要用set方法给属性设置值;
User模型类代码:
public class User {
private Integer id; //用户id
private String username; //用户名
private String password; //用户密码
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
}
数据操作dao接口IuserDao
public interface IUserDao {
/**
* 传入User类的一个实例
* @param user
*/
void add(User user);
}
数据操作dao实现类IUserDaoImpl
public class IUserDaoImpl implements IUserDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public void add(User user) {
System.out.println("dao添加用户"+user);
jdbcTemplate.update("insert into t_user (id,username,password) values(?,?,?)",user.getId(),user.getUsername(), user.getPassword());
}
}
再写一个测试类用于测试上述代码是否能正确连接数据库并插入上述一条数据。
JUnit4包的链接:链接: https://pan.baidu.com/s/1ArOw391g8k8GwHvFnjWCPQ 提取码: a6uh
public class demo1 {
@Test
public void test2(){
//得到beans.xml
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//获取beans.xml文件中id为"userDao"的bean
IUserDao userDao = (IUserDao)context.getBean("userDao");
//实例化User模型类
User user = new User();
//通过set方法插入数据
user.setId(null);
user.setUsername("meizu");
user.setPassword("meizu");
//将设置好数据后的User实例作为参数传入数据操作实现类的add方法中
userDao.add(user);
}
}
测试结果:
可以看出数据已经成功插入进去。
第二种:通过c3p0连接池插入数据
这种方法不需要改动代码,只需要将beans.xml文件中的部分属性值改一下即可,上代码:
<?xml version="1.0" encoding="UTF-8"?>
<!--xmlns xml namespace:xml命名空间-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p ="http://www.springframework.org/schema/p"
xmlns:context ="http://www.springframework.org/schema/context"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置c3p0连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///day18"/>
<property name="user" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--配置dbcp连接池-->
<!--<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">-->
<!--<property name="driverClassName" value="com.mysql.jdbc.Driver"/>-->
<!--<property name="url" value="jdbc:mysql://localhost:3306/day18"/>-->
<!--<property name="username" value="root"/>-->
<!--<property name="password" value="123456"/>-->
<!--</bean>-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="userDao" class="com.xianzhou.dao.Impl.IUserDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
</beans>
不同的地方已标出,可直观对比一下。
然后测试一下输出结果:
可以看到数据已正确插入,以上就是JdbcTemplate简单使用的几个方法,第一个通过API调用,虽然简单,但是数据库配置写的比较死,当你某一天改变数据库的账号、密码、库名、表名等时候,还要进入代码里面修改,稍有闪失就会出现修改错误的情况;而使用beans.xml配置就很自由,当配置改变的时候,只需要改变xml中的配置信息即可,不需要改动代码。以后还会说通过properties来配置数据库等连接信息。