其实这是更近一步的简化,不用每次都创建链接数据库的资源了,现在使用的是在tomcat服务器段配置数据源的方式,其实变得更加的高效的简便了。
需求:同样还是查询用户列表信息
applicationContext.xml配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:p="http://www.springframework.org/schema/p" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xmlns:tx="http://www.springframework.org/schema/tx" 8 xsi:schemaLocation="http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context-3.2.xsd 12 http://www.springframework.org/schema/tx 13 http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 14 http://www.springframework.org/schema/aop 15 http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> 16 17 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 18 <property name="location"> 19 <value>classpath:database.properties</value> 20 </property> 21 </bean> 22 <!-- 配置DataSource --> 23 <!-- <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> 24 <property name="driverClassName" value="${jdbc.driver}" /> 25 <property name="url" value="${jdbc.url}" /> 26 <property name="username" value="${jdbc.username}" /> 27 <property name="password" value="${jdbc.password}" /> 28 </bean> --> 29 <!-- 配置jndi的数据源 --> 30 <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 31 <property name="jndiName"> 32 <value>java:comp/env/jndi/smbms</value> 33 </property> 34 </bean> 35 36 <!-- 配置SqlSessionFactoryBean --> 37 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 38 <!-- 引用数据源组件 --> 39 <property name="dataSource" ref="dataSource" /> 40 <!-- 引用MyBatis配置文件中的配置 --> 41 <property name="configLocation" value="classpath:mybatis-config.xml" /> 42 </bean> 43 <!-- 配置DAO --> 44 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 45 <property name="basePackage" value="cn.smbms.dao" /> 46 </bean> 47 48 <context:component-scan base-package="cn.smbms.service" /> 49 <!-- 定义事务管理器 --> 50 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 51 <property name="dataSource" ref="dataSource"></property> 52 </bean> 53 <tx:annotation-driven /> 54 55 56 </beans>
编写测试类:
1 package cn.smbms.test.user; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.util.ArrayList; 6 import java.util.List; 7 8 import javax.servlet.ServletException; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 import org.apache.log4j.Logger; 14 import org.springframework.context.ApplicationContext; 15 import org.springframework.context.support.ClassPathXmlApplicationContext; 16 17 import cn.smbms.pojo.User; 18 import cn.smbms.service.user.UserService; 19 20 public class UserTestServlet extends HttpServlet { 21 22 private Logger logger = Logger.getLogger(UserTestServlet.class); 23 /** 24 * Constructor of the object. 25 */ 26 public UserTestServlet() { 27 super(); 28 } 29 30 /** 31 * Destruction of the servlet. <br> 32 */ 33 public void destroy() { 34 super.destroy(); // Just puts "destroy" string in log 35 // Put your code here 36 } 37 38 /** 39 * The doGet method of the servlet. <br> 40 * 41 * This method is called when a form has its tag value method equals to get. 42 * 43 * @param request the request send by the client to the server 44 * @param response the response send by the server to the client 45 * @throws ServletException if an error occurred 46 * @throws IOException if an error occurred 47 */ 48 public void doGet(HttpServletRequest request, HttpServletResponse response) 49 throws ServletException, IOException { 50 51 ApplicationContext ctx = new ClassPathXmlApplicationContext( 52 "applicationContext.xml"); 53 UserService userService = (UserService) ctx.getBean("userService"); 54 List<User> userList = new ArrayList<User>(); 55 User userCondition = new User(); 56 userCondition.setUserName("赵"); 57 userCondition.setUserRole(3); 58 userList = userService.findUsersWithConditions(userCondition); 59 60 for (User userResult : userList) { 61 logger.debug("testGetUserList userCode: " 62 + userResult.getUserCode() + " and userName: " 63 + userResult.getUserName() + " and userRole: " 64 + userResult.getUserRole() + " and userRoleName: " 65 + userResult.getUserRoleName() + " and address: " 66 + userResult.getAddress()); 67 } 68 } 69 70 /** 71 * The doPost method of the servlet. <br> 72 * 73 * This method is called when a form has its tag value method equals to post. 74 * 75 * @param request the request send by the client to the server 76 * @param response the response send by the server to the client 77 * @throws ServletException if an error occurred 78 * @throws IOException if an error occurred 79 */ 80 public void doPost(HttpServletRequest request, HttpServletResponse response) 81 throws ServletException, IOException { 82 83 response.setContentType("text/html"); 84 PrintWriter out = response.getWriter(); 85 out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); 86 out.println("<HTML>"); 87 out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); 88 out.println(" <BODY>"); 89 out.print(" This is "); 90 out.print(this.getClass()); 91 out.println(", using the POST method"); 92 out.println(" </BODY>"); 93 out.println("</HTML>"); 94 out.flush(); 95 out.close(); 96 } 97 98 /** 99 * Initialization of the servlet. <br> 100 * 101 * @throws ServletException if an error occurs 102 */ 103 public void init() throws ServletException { 104 // Put your code here 105 } 106 107 }
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="3.0" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 7 <display-name></display-name> 8 <servlet> 9 <description>This is the description of my J2EE component</description> 10 <display-name>This is the display name of my J2EE component</display-name> 11 <servlet-name>UserTestServlet</servlet-name> 12 <servlet-class>cn.smbms.test.user.UserTestServlet</servlet-class> 13 </servlet> 14 15 <servlet-mapping> 16 <servlet-name>UserTestServlet</servlet-name> 17 <url-pattern>/servlet/UserTestServlet</url-pattern> 18 </servlet-mapping> 19 <welcome-file-list> 20 <welcome-file>index.jsp</welcome-file> 21 </welcome-file-list> 22 </web-app>
输入运行地址:
最终结果: