java框架--spring+stutrs2+mybatis整合
代码区
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.sxt.action; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import cn.sxt.service.UserService; import cn.sxt.vo.User; @Controller("userAction") @Scope("prototype") public class UserAction { private List<User> list; @Autowired private UserService userService; public String list(){ list = userService.getAll(); return "success"; } public List<User> getList() { return list; } public void setList(List<User> list) { this.list = list; } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.sxt.dao; import java.util.List; import cn.sxt.vo.User; public interface UserDao { public List<User> getAll(); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.sxt.dao.impl; import java.util.List; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.support.SqlSessionDaoSupport; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import cn.sxt.dao.UserDao; import cn.sxt.vo.User; @Repository("userDao") public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{ @Autowired @Override public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { super.setSqlSessionFactory(sqlSessionFactory); } public List<User> getAll() { return this.getSqlSession().selectList("cn.sxt.vo.user.mapper.getAll"); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.sxt.service; import java.util.List; import cn.sxt.vo.User; public interface UserService { public List<User> getAll(); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.sxt.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import cn.sxt.dao.UserDao; import cn.sxt.service.UserService; import cn.sxt.vo.User; @Service("userService") public class UserServiceImpl implements UserService{ @Autowired private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public List<User> getAll() { return userDao.getAll(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.sxt.test; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.sxt.dao.UserDao; public class Test { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = (UserDao)ac.getBean("userDao"); List list = userDao.getAll(); System.out.println(list.size()); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.sxt.vo; public class User { private int id; private String name; private String pwd; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.sxt.vo.user.mapper"> <select id="getAll" resultType="User"> select * from user </select> </mapper>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/orcl"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <!-- 声明式事务配置 开始 --> <!-- 配置事务管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置事务通知 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <!-- 配置哪些方法使用什么样的事务,配置事务的传播特性 --> <tx:method name="add" propagation="REQUIRED"/> <tx:method name="insert" propagation="REQUIRED"/> <tx:method name="update" propagation="REQUIRED"/> <tx:method name="delete" propagation="REQUIRED"/> <tx:method name="remove*" propagation="REQUIRED"/> <tx:method name="get" read-only="true"/> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* cn.sxt.service.impl.*.*(..))" id="pointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> </aop:config> <!-- 声明式事务配置 结束 --> <!-- 配置sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis.cfg.xml"></property> </bean> <!-- 自动扫描 所配置包下的所有注解 dao-@Repository("userDao") service-@Service("userService") action-@Controller("userAction") 属性的注入:@autowired --> <context:component-scan base-package="cn.sxt"/> </beans>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <package name="cn.sxt.vo"/> </typeAliases> <mappers> <mapper resource="cn/sxt/vo/user.mapper.xml"/> </mappers> </configuration>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="default" extends="struts-default" namespace="/"> <action name="list" class="userAction" method="list"> <result>/list.jsp</result> </action> </package> </struts>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>17ssm_annotation</display-name> <!-- spring监听器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- struts2 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <table width="80%" align="center"> <tr> <td>编号</td> <td>姓名</td> <td>密码</td> </tr> <c:forEach items="${list }" var="bean"> <tr> <td>${bean.id }</td> <td>${bean.name }</td> <td>${bean.pwd }</td> </tr> </c:forEach> </table> </body> </html>