------------恢复内容开始------------
<!--配置数据库连接池--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="jdbc:mysql://localhost:3306/spring"/> <property name="username" value="root"/> <property name="password" value="root"/> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> </bean>
<!--JDBCTemplate对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入DataSource-->
<property name="dataSource" ref="dataSource"/>
</bean>
创建server类,创建dao类,在dao注入JDBCTemplate对象
开启组件扫描后
Server类创建对象并且注入Dao
@Service public class BookService { //注入dao @Autowired private BookDao bookDao; }
Dao中注入JDBTemplate对象,用来进行数据库的操作
@Repository public class BookDaoImpl implements BookDao { //注入jdbcTemplate @Autowired private JdbcTemplate jdbcTemplate; }
使用创建好的JDBCTemplate进行数据操作
创建一个数据库,在数据库中创建一个表:
create table t_book ( user_id int not null primary key auto_increment, username varchar(100) not null , ustatus varchar(50) not null )
对应数据库表,创建一个实体类
public class Book { private String userId; private String username; private String ustatus; ? public String getUserId() { return userId; } ? public String getUsername() { return username; } ? public String getUstatus() { return ustatus; } ? public void setUserId(String userId) { this.userId = userId; } ? public void setUsername(String username) { this.username = username; } ? public void setUstatus(String ustatus) { this.ustatus = ustatus; } }
编写Service和dao,在dao进行数据库的添加操作
调用jdbcTemplate对象里面的update()方法来进行数据库的添加操作
有两个参数,是一个参数SQL语句,第二个参是可变参数(SQL语句中的值)
实现数据的添加:
@Repository public class BookDaoImpl implements BookDao { //注入jdbcTemplate @Autowired private JdbcTemplate jdbcTemplate; ? @Override public void add(Book book) { //创建SQL语句 String sql = "insert into t_book(username,ustatus) values(?,?)"; //调用方法实现update.(sql,args) int update = jdbcTemplate.update(sql, book.getUsername(), book.getUstatus()); System.out.println(update); } }
编写一个测试类(测试增加数据)
public class testBook { @Test public void testJdbcTemplate(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); BookService bookService = (BookService)context.getBean("bookService"); //创建book对象 Book book = new Book(); //传入对象中的值 book.setUsername("测试用户名"); book.setUstatus("成功"); //调用增加方法 bookService.addBook(book); } }
测试结果(数据写入成功~)
七月 13, 2020 4:35:17 下午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info 信息: {dataSource-1} inited 1
查询数据库表可看到
select * from t_book
数据库的添加操作(完成~)
实现数据的修改
创建修改方法
@Override public void update(Book book) { String SQL = "update t_book set username=?,ustatus=? where user_id=?"; int update = jdbcTemplate.update(SQL, book.getUsername(), book.getUstatus(), book.getUserId()); System.out.println(update); }
编写一个测试类(测试修改数据)
public class testBook { @Test public void testJdbcTemplate(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); BookService bookService = (BookService)context.getBean("bookService"); Book book = new Book(); book.setUserId("1"); book.setUsername("修改测试"); book.setUstatus("yes~"); //调用修改方法 bookService.updateBook(book); } }
测试结果(数据修改成功~)
七月 13, 2020 4:47:24 下午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info 信息: {dataSource-1} inited 1
查询数据库表可看到
select * from t_book
数据库的修改操作(完成~)
实现数据的删除
创建删除方法
@Override public void delete(String id) { String SQL = "delete from t_book where user_id=?"; int update = jdbcTemplate.update(SQL, id); System.out.println(update); }
编写一个测试类(测试删除数据)
public class testBook { @Test public void testJdbcTemplate(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); BookService bookService = (BookService)context.getBean("bookService"); bookService.deleteBook("1"); } }
测试结果(数据删除成功~)
七月 13, 2020 4:56:58 下午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info 信息: {dataSource-1} inited 1
查询数据库表可看到
select * from t_book
数据库的删除操作(完成~)
以上为使用jdbctemplate进行数据库的增删改功能~
以下为全部源码
Bean.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" 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"> ? <!--组件扫描服务开启--> <context:component-scan base-package="com.minelsg"/> <!--数据库连接池--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="jdbc:mysql://localhost:3306/spring"/> <property name="username" value="root"/> <property name="password" value="rzt123123"/> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> </bean> ? <!--JDBC Template对象--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <!--注入DataSource--> <property name="dataSource" ref="dataSource"/> </bean> </beans>
dao层文件
public interface BookDao { //添加的方法 void add(Book book); ? void update(Book book); ? void delete(String id); } @Repository public class BookDaoImpl implements BookDao { //注入jdbcTemplate @Autowired private JdbcTemplate jdbcTemplate; //增加操作 @Override public void add(Book book) { //创建SQL语句 String sql = "insert into t_book(username,ustatus) values(?,?)"; //调用方法实现update.(sql,args) int update = jdbcTemplate.update(sql, book.getUsername(), book.getUstatus()); System.out.println(update); } //修改操作 @Override public void update(Book book) { String SQL = "update t_book set username=?,ustatus=? where user_id=?"; int update = jdbcTemplate.update(SQL, book.getUsername(), book.getUstatus(), book.getUserId()); System.out.println(update); } //删除操作 @Override public void delete(String id) { String SQL = "delete from t_book where user_id=?"; int update = jdbcTemplate.update(SQL, id); System.out.println(update); } }
entity层文件
public class Book { private String userId; private String username; private String ustatus; ? public String getUserId() { return userId; } ? public String getUsername() { return username; } ? public String getUstatus() { return ustatus; } ? public void setUserId(String userId) { this.userId = userId; } ? public void setUsername(String username) { this.username = username; } ? public void setUstatus(String ustatus) { this.ustatus = ustatus; } }
Server层文件
测试类
public class testBook { @Test public void testJdbcTemplate(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); BookService bookService = (BookService)context.getBean("bookService"); Book book = new Book(); //book.setUsername("测试用户名"); //book.setUstatus("成功"); //bookService.addBook(book); //book.setUserId("1"); //book.setUsername("修改测试"); //book.setUstatus("yes~"); bookService.deleteBook("1"); } }
Done~
------------恢复内容结束------------