012_JDBC模板
生活随笔
收集整理的這篇文章主要介紹了
012_JDBC模板
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一. Spring的JDBC的模板
1. Spring對持久層也提供了解決方案: ORM模塊和JDBC的模板。
2. Spring提供了很多的模板用于簡化開發:
3. Spring的JDBC模板的jar包:
二. Spring的JDBC的模板簡單使用例子
1. 新建一個名為SpringJdbc的Java工程, 同時拷入相關jar包
2. 創建SpringJdbc.java?
package com.lywgames.springjdbc;import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource;public class SpringJdbc {public static void main(String[] args) {// 創建連接池DriverManagerDataSource dataSource = new DriverManagerDataSource();dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");dataSource.setUrl("jdbc:mysql://192.168.25.138:3306/studyjdbc?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai");dataSource.setUsername("root");dataSource.setPassword("lyw123456");// 創建jdbc模板JdbcTemplate jdbcTemplate = new JdbcTemplate();jdbcTemplate.setDataSource(dataSource);Long count = jdbcTemplate.queryForObject("select count(*) from user", Long.class);System.out.println("count = " + count);} }3. 查看user表
4. 運行項目
三. Spring的JDBC的模板屬性注入方式例子
1. 新建一個名為SpringJdbcProperty的Java工程, 同時拷入相關jar包
2. 創建User.java
package com.lywgames.springjdbc;import java.io.Serializable; import java.sql.Timestamp;public class User implements Serializable {private static final long serialVersionUID = 1L;private Integer id;private String username;private String password;private Timestamp registertime;public User() {}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;}public Timestamp getRegistertime() {return registertime;}public void setRegistertime(Timestamp registertime) {this.registertime = registertime;}@Overridepublic String toString() {return "id=" + id + ",username=" + username + ",password=" + password + ",registertime=" + registertime;}}3. 創建SpringJdbc.java
package com.lywgames.springjdbc;import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** Spring單元測試, 還必須引入aop模塊*/ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class SpringJdbc {// 注入Jdbc模板屬性@Resource(name = "jdbcTemplate")private JdbcTemplate jdbcTemplate;@Testpublic void getAdminUserList() {List<User> list = jdbcTemplate.query("select * from user", new MyRowMapper());for (User user : list) {System.out.println(user);}}public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);User user = jdbcTemplate.queryForObject("select * from user where id=?", new MyRowMapper(), 1000);System.out.println(user);context.close();}}class MyRowMapper implements RowMapper<User>{@Overridepublic User mapRow(ResultSet rs, int rowNum) throws SQLException {User account = new User();account.setId(rs.getInt("id"));account.setUsername(rs.getString("username"));account.setPassword(rs.getString("password"));account.setRegistertime(rs.getTimestamp("registertime"));return account;} }4. 在src目錄下創建jdbc.properties數據庫連接配置文件
5. 在src目錄下創建applicationContext.xml
6. 引入JUnit
7. 運行項目
8. JUnit運行項目
總結
以上是生活随笔為你收集整理的012_JDBC模板的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 011_AOP注解开发
- 下一篇: 013_JDBC模板使用第三方连接池