Java jdbctemplate赋值_Spring 之 Spring JdbcTemplate的使用
1.準(zhǔn)備
源碼下載鏈接
https://share.weiyun.com/Iax0hI64
0. 數(shù)據(jù)表
create table user(
uid int not null PRIMARY key AUTO_INCREMENT,
username VARCHAR(20) not null,
password VARCHAR(20) not null
)ENGINE=INNODB DEFAULT CHARSET=utf8;
insert into user values(1,"zhenqk","12345");
insert into user values(2,"sun","12345");
insert into user values(3,"小白","123");
insert into user values(4,"zhen","12345");
1.導(dǎo)入依賴
junit
junit
4.11
test
org.springframework
spring-context
5.2.1.RELEASE
org.springframework
spring-aspects
5.2.1.RELEASE
.org.springframework
spring-jdbc
5.2.1.RELEASE
pom
com.alibaba
druid
1.1.9
mysql
mysql-connector-java
5.1.47
2.db.properties
druid.driver=com.mysql.jdbc.Driver
druid.url=jdbc:mysql://localhost:3306/kdb2
druid.username=root
druid.password=root
3.applicationContext.xml
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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd ">
2.實(shí)體類User.java
main/java/com/ccut/pojo/User.java
public class User {
private int uid;
private String username;
private String password;
@Override
public String toString() {
return "User{" +
"uid=" + uid +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
public User() {
}
public User(int uid, String username, String password) {
this.uid = uid;
this.username = username;
this.password = password;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
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;
}
}
3.測試方法使用
import java.util.List;
public class Test {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicatinContext.xml");
JdbcTemplate jdbcTemplate1 = context.getBean("jdbcTemplate", JdbcTemplate.class);
@org.junit.Test
public void testDB(){
System.out.println(jdbcTemplate1);
}
/**
* DML語句
* 使用的是共同的update方法,只需要替換不同的sql即可
*/
@org.junit.Test
public void testUpdate(){
String sql = "update user set username=?,password=? where uid=?";
//參數(shù)賦值按照占位符順序
jdbcTemplate1.update(sql,"jeck","abc",3);
}
/**
* DQL語句
*/
@org.junit.Test
public void testqueryObject(){//獲取到一條記錄通過一個Java中實(shí)體類對象進(jìn)行接收
String sql = "select uid,username,password from user where uid=?";
//將數(shù)據(jù)庫查詢出來的條數(shù)據(jù)通過RowMapper對象進(jìn)行封裝為泛型類型
RowMapper rowMapper = new BeanPropertyRowMapper(User.class);
//參數(shù)賦值按照占位符順序
User user = jdbcTemplate1.queryForObject(sql, rowMapper, 2);
System.out.println(user);
}
@org.junit.Test
public void testqueryValue(){//獲取到一個獨(dú)立的數(shù)據(jù)
String sql = "select count(uid) from user";
//參數(shù)賦值按照占位符順序
int count = jdbcTemplate1.queryForObject(sql, Integer.class);
System.out.println(count);
}
@org.junit.Test
public void testqueryList(){//獲取到多條記錄,通過list集合進(jìn)行接收查詢數(shù)據(jù)
String sql = "select uid,username,password from user";
RowMapper rowMapper = new BeanPropertyRowMapper(User.class);
//參數(shù)賦值按照占位符順序
List userList = jdbcTemplate1.query(sql, rowMapper);
System.out.println(userList);
}
// 測試 事務(wù)
@org.junit.Test
public void testUserDao0(){
UserDao userDaoImpl = context.getBean("userDaoImpl", UserDao.class);
User u=new User(1,"王振強(qiáng)","12345");
Boolean b=userDaoImpl.testUpdate(u);
System.out.println(b);
}
}
4. 與dao結(jié)合使用
1.dao層
IUserDao.java
public interface UserDao {
/**
* 獲取一條記錄
*/
User testqueryObject(Integer uid);
Boolean testUpdate(User user);
}
Impl.UserDaoImpl.java
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public User testqueryObject(Integer uid) {
String sql = "select uid,username,password from user where uid=?";
//將數(shù)據(jù)庫查詢出來的條數(shù)據(jù)通過RowMapper對象進(jìn)行封裝為泛型類型
RowMapper rowMapper = new BeanPropertyRowMapper(User.class);
//參數(shù)賦值按照占位符順序
return jdbcTemplate.queryForObject(sql, rowMapper, uid);
}
@Override
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRES_NEW)
public Boolean testUpdate(User user){
String sql = "update user set username=?,password=? where uid=?";
//參數(shù)賦值按照占位符順序
// 異常代碼 int o=3/0;
int a=jdbcTemplate.update(sql,user.getUsername(),user.getPassword(),user.getUid());
if(a>0){
return true;
}
return false;
}
}
2. test類
/**
* 測試在dao層使用spring中JDBC模塊
*/
@org.junit.Test
public void testUserDao(){
UserDao userDaoImpl = context.getBean("userDaoImpl", UserDao.class);
User user = userDaoImpl.testqueryObject(2);
System.out.println(user);
}
總結(jié)
以上是生活随笔為你收集整理的Java jdbctemplate赋值_Spring 之 Spring JdbcTemplate的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java的图形界面上传附件_Java图形
- 下一篇: java使用druid maven_Sp