目錄
1、JdbcTemplate簡介
- spring提供用於操作JDBC工具類,類似:DBUtils
- 依賴 連接池DataSource(數據源)
2、搭建環境
2.1、創建表
創建一張t_user表,並插入數據 create table t_user( id int primary key auto_increment, username varchar(50), password varchar(32) ); insert into t_user values(1,'jack','1234'),(2,'rose','5678');
2.2、需要的jar包
2.3、javabean
/* create table t_user( id int primary key auto_increment, username varchar(50), password varchar(32) ); */ public class User { private Integer id; private String username; private String password; public Integer getId(){ return id; } public String getUsername(){ return username; } public String getPassword(){ return password; } public void setId(Integer id){ this.id = id; } public void setUsername(String username){ this.username = username; } public void setPassword(String password){ this.password = password; } @Override public String toString() { return "User [id=" + id + ",username=" + username + ",password=" + password + "]"; } }
3、使用api(瞭解)
public class TestApi { public static void main(String[] args) { //1、創建數據源(連接池)使用dbcp BasicDataSource dataSource = new BasicDataSource(); //基本四要素: dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/spring_jdbc"); dataSource.setUsername("root"); dataSource.setPassword("hmx123456"); //2、創建模版 JdbcTemplate jdbcTemplate = new JdbcTemplate(); jdbcTemplate.setDataSource(dataSource); //3、通過api操作(插入信息) jdbcTemplate.update("insert into t_user(username,password) values ('?','?')","tom","3333"); } }
4、配置DBCP
4.1、Dao層
public class UserDao { //jdbc的模版將由spring注入 提供setter 和 getter方法 private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public void update(User user){ String sql = "update t_user set username = ?,password = ? where id = ?"; Object[] args = {user.getUsername(),user.getPassword(),user.getId()};//參數 jdbcTemplate.update(sql,args); } }
4.2、配置文件
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--配置數據源 BasicDataSource dataSource = new BasicDataSource(); 1、dataSource.setDriverClassName("com.mysql.jdbc.Driver"); 2、dataSource.setUrl("jdbc:mysql://localhost:3306/spring_jdbc"); 3、dataSouruce.setUsername("root"); 4、dataSource.setPassword("hmx123456"); --> <bean id = "dataSourceId" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value = "com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/spring_jdbc"></property> <property name="username" value = "root"></property> <property name="password" value = "hmx123456"></property> </bean> <!--創建模版 JdbcTemplate jdbcTemplate = new JdbcTemplate(); jdbcTemplate.setDataSource(dataSource); --> <bean id = "jdbcTemplateId" class = "org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref = "dataSourceId"></property> </bean> <!--配置dao private JdbcTemplate jdbcTemplate; --> <bean id = "userDaoId" class="test_jdbc_c_dbcp.UserDao"> <property name="jdbcTemplate" ref = "jdbcTemplateId"></property> </bean> </beans>
4.3、測試類
public class TestDBCP { @Test public void demo01(){ User user = new User(); user.setId(1); user.setUsername("jack01"); user.setPassword("11111"); String xmlPath = "test_jdbc_c_dbcp/applicationContext.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); UserDao userDao = applicationContext.getBean("userDaoId",UserDao.class); userDao.update(user); } }
5、配置C3P0
5.1、Dao層
public class UserDao { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } //更新數據庫操作 public void update(User user){ String sql = "update t_user set username = ?,password = ? where id = ? ";//sql語句 Object[] args = {user.getUsername(),user.getPassword(),user.getId()};//參數 jdbcTemplate.update(sql,args); } //查詢所有 public List<User> selectAll(){ //String sql = "select * from t_user";//查詢所有的sql語句 return jdbcTemplate.query("select * from t_user", BeanPropertyRowMapper.newInstance(User.class)); } }
5.2、配置文件
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--配置C3P0數據源 BasicDataSource dataSource = new BasicDataSource(); 1、dataSource.setDriverName("com.mysql.jdbc.Driver"); 2、dataSource.setJdbcurl("jdbc:mysql://localhost:3306/spring_jdbc"); 3、dataSouruce.setUser("root"); 4、dataSource.setPassword("hmx123456"); c3p0要配置的屬性名與DBCP不同,內容相同 --> <bean id = "dataSourceId" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value = "jdbc:mysql://localhost:3306/spring_jdbc"></property> <property name="user" value = "root"></property> <property name="password" value = "hmx123456"></property> </bean> <!--創建模版 JdbcTemplate jdbcTemplate = new JdbcTemplate(); jdbcTemplate.setDataSource(dataSource); --> <bean id = "jdbcTemplateId" class = "org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref = "dataSourceId"></property> </bean> <!--配置dao private JdbcTemplate jdbcTemplate; --> <bean id = "userDaoId" class="test_jdbc_d_c3p0.UserDao"> <property name="jdbcTemplate" ref = "jdbcTemplateId"></property> </bean> </beans>
5.3、測試類
public class TestC3P0 { @Test public void demo01(){ User user = new User(); user.setId(1);; user.setUsername("jack01"); user.setPassword("11111"); String xmlPath = "test_jdbc_d_c3p0/applicationContext.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); UserDao userDao = applicationContext.getBean("userDaoId",UserDao.class); userDao.update(user); List<User> list = userDao.selectAll(); for(User x : list){ System.out.println(x); } } }
6、使用JdbcDaoSupport
6.1、Dao層
public class UserDao extends JdbcDaoSupport { /* JdbcDaoSupport類中提供了模版,直接拿過來用就行 子類繼承父類:使用this或者super都可以 this是先找子類,子類沒有再找父類,super是直接找父類 */ public void update(User user){ String sql = "update t_user set username = ?,password = ? where id = ?"; Object[] args = {user.getUsername(),user.getPassword(),user.getId()}; super.getJdbcTemplate().update(sql,args); } public List<User> selectAll(){ String str = "select * from t_user"; return super.getJdbcTemplate().query(str, BeanPropertyRowMapper.newInstance(User.class)); } }
6.2、配置文件
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--配置數據源c3p0 BasicDataSource dataSource = new BasicDataSource(); 1、dataSource.setDriverClassName("com.mysql.jdbc.Driver"); 2、dataSource.setUrl("jdbc:mysql://localhost:3306/spring_jdbc"); 3、dataSouruce.setUsername("root"); 4、dataSource.setPassword("hmx123456"); --> <bean id = "dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring_jdbc"></property> <property name="user" value="root"></property> <property name="password" value="hmx123456"></property> </bean> <!-- 配置dao dao層繼承了JdbcDaoSupport,之後只需要注入數據源,底層將自動的創建模版 可以查看setDataSource方法的源碼 --> <bean id="userDaoId" class="test_jdbc_e_jdbcdaosupport.UserDao"> <property name="dataSource" ref = "dataSource"></property> </bean> </beans>
6.3、測試類
public class TestJdbcDaoSupport { @Test public void demo(){ User user = new User(); user.setId(2); user.setUsername("rose"); user.setPassword("2222"); String xmlPath = "test_jdbc_e_jdbcdaosupport/applicationContext.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); UserDao userDao = applicationContext.getBean("userDaoId",UserDao.class); userDao.update(user); List<User> list = userDao.selectAll(); for(User x:list){ System.out.println(x); } } }
7、配置properties
7.1、properties文件
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql://localhost:3306/spring_jdbc jdbc.user=root jdbc.password=hmx123456
7.2、Dao層
public class UserDao extends JdbcDaoSupport { public List<User> selectAll(){ return super.getJdbcTemplate().query("select * from t_user", BeanPropertyRowMapper.newInstance(User.class)); } //通過id查詢 public User getById(int i){ return super.getJdbcTemplate().queryForObject("select * from t_user where id = ?",BeanPropertyRowMapper.newInstance(User.class),1); } }
7.3、配置文件
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--配置數據源c3p0 BasicDataSource dataSource = new BasicDataSource(); 1、dataSource.setDriverClassName("com.mysql.jdbc.Driver"); 2、dataSource.setUrl("jdbc:mysql://localhost:3306/spring_jdbc"); 3、dataSouruce.setUsername("root"); 4、dataSource.setPassword("hmx123456"); --> <!--加載配置文件 classpath:該前綴表示在src下 在配置文件之後,通過${key}獲得 --> <context:property-placeholder location="classpath:test_jdbc_f_properties/jdbcInfo.properties"></context:property-placeholder> <bean id = "dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 配置dao dao層繼承了JdbcDaoSupport,之後只需要注入數據源,底層將自動的創建模版 可以查看setDataSource方法的源碼 --> <bean id="userDaoId" class="test_jdbc_f_properties.UserDao"> <property name="dataSource" ref = "dataSource"></property> </bean> </beans>
7.4、測試類
public class TestProperties { @Test public void demo(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("test_jdbc_f_properties/applicationContext.xml"); UserDao userDao = (UserDao)applicationContext.getBean("userDaoId"); System.out.println(userDao.getById(1)); } }