通过配置文件XML方法的配置
可以使用非常简练的Service类
UserService类代码如下:
package com.swift; public class UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public boolean add() { return userDao.add(); } }
UserService要调用UserDao中连接数据库的方法add(),需要一个userDao对象,这个对象通过配置文件XML来注入对象,需要setUserDao()来配合
同样UserDao类也非常简练
package com.swift; import org.springframework.jdbc.core.JdbcTemplate; public class UserDao { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public boolean add() { // ComboPooledDataSource dataSource=new ComboPooledDataSource(); // dataSource.setDriverClass("com.mysql.jdbc.Driver"); // dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/sw_database"); // dataSource.setUser("root"); // dataSource.setPassword("root"); String sql="insert into sw_user(username,password) values(?,?)"; int count=jdbcTemplate.update(sql,"弹痕","战侠歌"); if(count==1) { return true; } return false; } }
UserDao中要使用JdbcTemplate的对象,这个对象也通过配置文件XML来注入,需要setJdbcTemplate()来配合
而jdbcTemplate对象又需要通过XML在配置中注入dataSource对象
dataSource对象是通过c3p0连接池类得到,需要导入前一篇随笔提供的jar包支持
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" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- c3p0连接池得到dataSource --> <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/sw_database"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="userDao" class="com.swift.UserDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <bean id="userService" class="com.swift.UserService"> <property name="userDao" ref="userDao"></property> </bean> </beans>
测试类中只要得到XML配置文件对象就可以搞定所有了
代码如下:
package com.swift; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @WebServlet("/test") public class ServletTest extends HttpServlet { private static final long serialVersionUID = 1L; public ServletTest() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); response.getWriter().append("Served at: ").append(request.getContextPath()); //使用JdbcTemplat的queryForObject方法 ApplicationContext context=new ClassPathXmlApplicationContext("c3p0.xml"); UserService userService= (UserService) context.getBean("userService"); if(userService.add()) { response.getWriter().append("添加成功!"); }else { response.getWriter().append("添加失败!"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
浏览器中结果:
数据库中结果:
原文地址:https://www.cnblogs.com/qingyundian/p/8127240.html
时间: 2024-10-14 03:07:15