JdbcTmplate中的update方法(代码)基础操作
生活随笔
收集整理的這篇文章主要介紹了
JdbcTmplate中的update方法(代码)基础操作
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
User.java實(shí)體類:
package com.liu.jdbc.update;public class User {private Integer 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;}@Overridepublic String toString() {return "User [id=" + id + ", username=" + username + ", password=" + password + "]";}}UserDao.java接口:
package com.liu.jdbc.update;public interface UserDao {public int addUser(User user);public int updateUser(User user);public int deleteUser(int id);}UserDaoImpl.java實(shí)現(xiàn)類:
package com.liu.jdbc.update;import org.springframework.jdbc.core.JdbcTemplate;public class UserDaoImpl implements UserDao {//聲明JdbcTemplate屬性及其setter方法private JdbcTemplate jdbcTemplate;public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;}//添加方法@Overridepublic int addUser(User user) {// TODO Auto-generated method stub//定義SQL語句String sql = "insert into user(username,password) values(?,?)";//定義數(shù)組來存放SQL中的參數(shù)Object[] obj = new Object[] {user.getUsername(),user.getPassword()};//執(zhí)行添加操作 返回的是受影響的行數(shù)int i = this.jdbcTemplate.update(sql, obj);return i;}//更新方法@Overridepublic int updateUser(User user) {// TODO Auto-generated method stub//定義SQL語句String sql = "update user set username = ?,password = ? where id = ?";//定義數(shù)組 存放SQL中的參數(shù)Object[] obj = new Object[] {user.getUsername(),user.getPassword(),user.getId()};//執(zhí)行更新操作 返回的是受影響的行數(shù)int i = this.jdbcTemplate.update(sql, obj);return i;}//刪除方法@Overridepublic int deleteUser(int id) {// TODO Auto-generated method stub//定義SQL語句String sql = "delete from user where id = ?";//執(zhí)行刪除操作 返回的是受影響的行數(shù)int i = this.jdbcTemplate.update(sql, id);return i;}}ApplicationContext.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.3.xsd"><!-- 配置數(shù)據(jù)源 --><bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource"><!-- 數(shù)據(jù)庫驅(qū)動(dòng) --><property name="driverClassName" value = "com.mysql.jdbc.Driver"></property><!-- 數(shù)據(jù)庫url --><property name="url" value = "jdbc:mysql://localhost:3306/test"></property><!-- 數(shù)據(jù)庫用戶名 --><property name="username" value = "root"></property><!-- 數(shù)據(jù)庫密碼 --><property name="password" value = "root"></property></bean><!-- 配置jdbc模板類 --><bean id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate"><!-- 默認(rèn)必須使用數(shù)據(jù)源 --><property name="dataSource" ref = "dataSource"></property></bean><!-- 定義ID為userDdao的bean --><bean id = "userDao" class = "com.liu.jdbc.update.UserDaoImpl"><property name="jdbcTemplate" ref ="jdbcTemplate"></property></bean></beans>Test.java測試類:
package com.liu.jdbc.update;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String args[]) {Test test = new Test();test.add(); // test.update(); // test.delete();}//添加操作public void add() {String xmlpath = "com/liu/jdbc/update/ApplicationContext.xml";ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlpath);UserDao userDao = (UserDao) applicationContext.getBean("userDao");User user = new User();user.setUsername("張三");user.setPassword("123456");int i = userDao.addUser(user);if(i>0) {System.out.println("添加操作成功,影響了"+i+"條數(shù)據(jù)");}else {System.out.println("添加操作失敗");}}//更新操作public void update() {String xmlpath = "com/liu/jdbc/update/ApplicationContext.xml";ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlpath);UserDao userDao = (UserDao) applicationContext.getBean("userDao");User user = new User();user.setUsername("李四");user.setPassword("111111");user.setId(1);int i = userDao.updateUser(user);if(i>0) {System.out.println("更新操作成功,影響了"+i+"條數(shù)據(jù)");}else {System.out.println("更新操作失敗");}}//刪除操作public void delete() {String xmlpath = "com/liu/jdbc/update/ApplicationContext.xml";ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlpath);UserDao userDao = (UserDao) applicationContext.getBean("userDao");int i = userDao.deleteUser(1);if(i>0) {System.out.println("刪除操作成功,影響了"+i+"條數(shù)據(jù)");}else {System.out.println("刪除操作失敗");}}}總結(jié)
以上是生活随笔為你收集整理的JdbcTmplate中的update方法(代码)基础操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JdbcTemplate类中的execu
- 下一篇: JdbcTemplate中的query方