步骤还是从web.xml开始_(:з」∠)_~ 可无视log4j~ლ(╹◡╹ლ)
<?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" version="2.5"> <context-param> <param-name>webAppRootKey</param-name> <param-value>ssi.root</param-value> </context-param> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>actionPackages</param-name> <param-value>com.cstor.network.action</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:resource/applicationContext*.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- log4j --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/classes/resource/log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60000</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> </web-app>
秉承先搞定配置文件的执着↓↓↓↓↓↓↓↓applicationContext.xml↓↓↓↓↓↓↓↓↓↓↓↓(数据源换成c3p0了)
<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <aop:aspectj-autoproxy proxy-target-class="true" /> <!-- dataSource --> <bean name="c3p0DS" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="jdbcUrl" value="jdbc:mysql://localhost/test"></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> </bean> <!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> <property name="maxActive" value="100"></property> <property name="maxIdle" value="30"></property> <property name="maxWait" value="500"></property> <property name="defaultAutoCommit" value="true"></property> </bean> --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="c3p0DS" /> </bean> <!-- dao --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="c3p0DS" /> <property name="typeAliasesPackage" value="com.wporoad.app.entity" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.dao" /> </bean> </beans>
然后需要mapper接口和xml↓↓↓↓↓↓↓↓
UserMapper.java
package com.dao; import java.util.List; import com.entity.User; public interface UserMapper { public void addUser(User user); public List<User> queryUsers(); public void delUser(String id); }
UserMapper.xml
<?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="com.dao.UserMapper"> <resultMap id="UserMap" type="com.entity.User"> <result property="id" column="ID" /> <result property="userName" column="USERNAME" /> <result property="password" column="PASSWORD" /> <result property="email" column="EMAIL" /> <result property="isbn" column="ISBN"/> </resultMap> <insert id="addUser" parameterType="User"> INSERT INTO USER( ID, USERNAME, PASSWORD, EMAIL, ISBN ) VALUES ( #{id}, #{userName}, #{password}, #{email}, #{isbn} ) </insert> <select id="queryUsers" resultMap="UserMap" > SELECT * FROM USER </select> <!-- 删除信息 --> <delete id="delUser" parameterType="string"> DELETE FROM USER WHERE ID = #{id} </delete> </mapper>
补上实体类User.java
package com.entity; import java.io.Serializable; public class User implements Serializable { private static final long serialVersionUID = 1L; private String id; private String userName; private String password; private String email; private String isbn; ........get、set........... }
继续写业务层的接口和实现↓↓↓↓↓↓↓↓↓↓↓
UserService.java
package com.service; import java.util.List; import com.entity.User; public interface UserService { public void addUser(User user); public List<User> queryUsers(); public void delUser(String id); }
UserServiceImpl.java
package com.serviceImpl; import java.util.List; import com.dao.UserMapper; import com.entity.User; import com.service.UserService; public class UserServiceImpl implements UserService{ private UserMapper userMapper; public UserMapper getUserMapper() { return userMapper; } public void setUserMapper(UserMapper userMapper) { this.userMapper = userMapper; } public void addUser(User user) { userMapper.addUser(user); } public List<User> queryUsers() { List<User> userList = userMapper.queryUsers(); return userList; } public void delUser(String id) { userMapper.delUser(id); } }
Actionლ(╹◡╹ლ) Actionლ(╹◡╹ლ) Actionლ(╹◡╹ლ)
UserAction.java
package com.web; import java.util.List; import java.util.UUID; import javax.servlet.http.HttpServletRequest; import org.apache.log4j.Logger; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; import com.entity.User; import com.service.UserService; public class UserAction extends ActionSupport { private static final long serialVersionUID = 1L; private static Logger logger = Logger.getLogger(UserAction.class); public UserService userService; public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } private String id; private String userName; private String password; private String email; private String isbn; List<User> userList; public String addUser() { User user = new User(); try { System.out.println(1); String iid = UUID.randomUUID().toString(); user.setId(iid); user.setUsername(userName); user.setPassword(password); user.setEmail(email); user.setIsbn(isbn); userService.addUser(user); System.out.println(2); } catch (Exception e) { logger.error("exception in add user", e); return ERROR; } return SUCCESS; } public String queryUsers() { try { userList = userService.queryUsers(); HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("list", userList); return "list"; } catch (Exception e) { logger.error("Exception in queryUsers", e); return ERROR; } } public String delUser() { try { userService.delUser(id); } catch (Exception e) { logger.error("Exception in delUser", e); return ERROR; } return SUCCESS; } ........get、set............ }
至此已一本Ok了,至于struts.xml←这货和页面什么的~自行YY即可√ლ(╹◡╹ლ)