S2sm搭建
1. 导入需要的jar包mybatis+spring+struct+mysql
2. 项目整体结构
3. 配置spring容器
applicationContext-dao.xml(主要用来配置数据源,sqlSession,mapper扫描器加载mapper)
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 启用spring注解支持 -->
<context:annotation-config/>
<!-- 配置DataSource数据源 -->
<beanid="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<propertyname="driverClassName"value="com.mysql.jdbc.Driver"/>
<propertyname="url"value="jdbc:mysql://localhost:3306/mybatis"/>
<propertyname="username"value="root"/>
<propertyname="password"value="123456"/>
</bean>
<!--创建sqlSessionFactory -->
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<propertyname="configLocation"value="classpath:mybatis/mybatis-config.xml"/>
<propertyname="dataSource"ref="dataSource"/>
</bean>
<!-- mapper自动扫描 -->
<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定扫描的包 -->
<propertyname="basePackage"value="com.mapper"/>
<!--sqlSessionFactory -->
<propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"/>
</bean>
</beans>
applicationContext-service.xml(配置业务逻辑层和action)
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 配置service -->
<beanid="userInfoService"class="com.hzs.service.UserInfoService"/>
<beanid="userService"class="com.hzs.service.UserService"/>
<beanid="userManageAction"class="com.hzs.action.UserManagerAction"/>
</beans>
applicationContext-transaction.xml(事务处理)
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 事务控制配置 -->
<beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 配置数据源 -->
<propertyname="dataSource"ref="dataSource"/>
</bean>
<!-- 通知 -->
<tx:adviceid="txAdvice"transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为 -->
<tx:methodname="save*"propagation="REQUIRED"/>
<tx:methodname="insert*"propagation="REQUIRED"/>
<tx:methodname="delete*"propagation="REQUIRED"/>
<tx:methodname="update*"propagation="REQUIRED"/>
<tx:methodname="find*"propagation="SUPPORTS"read-only="true"/>
<tx:methodname="get*"propagation="SUPPORTS"read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:advisoradvice-ref="txAdvice"
pointcut="execution(* com.hzs.service.*.*(..))"/>
</aop:config>
</beans>
4.根据数据库要求建模
package com.hzs.pojo;
public classUserInfo {
private intid;
private Stringusername;
private Stringpassword;
private Stringismanager;
public UserInfo() {
super();
// TODO Auto-generated constructor stub
}
public UserInfo(intid, String username, Stringpassword, String ismanager) {
super();
this.id =id;
this.username =username;
this.password =password;
this.ismanager =ismanager;
}
public int getId() {
return id;
}
public void setId(intid) {
this.id =id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username =username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password =password;
}
public String getIsmanager() {
return ismanager;
}
public void setIsmanager(String ismanager) {
this.ismanager =ismanager;
}
}
5.编写数据库访问层,在spring容器中配置了数据源见上面的applicationContext-dao.xml,使用mybatis,使用mapper方式,将xxxMapper.java和xxxMapper.xml放在同一个目录下面,里面的方法名要求对应相同,xxxMapper.java必须是接口类型
UserInfoMapper.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEmapper PUBLIC "-//mybatis.org//DTDMapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mappernamespace="com.mapper.UserInfoMapper">
<!-- 自动生成id策略 -->
<insertid="insertUser" parameterType="userinfo" useGeneratedKeys="true" keyProperty="id">
insert into userinfo(username,password,ismanager)values (#{username},#{password},#{ismanager})
</insert>
<!-- userInfoResultMap是userinfo-resultmap.xml中定义的resultmap -->
<selectid="getList" parameterType="userinfo"resultType="list"resultMap="userInfoResultMap">
select * from userinfo whereusername like '%' #{username} '%'
</select>
<selectid="getAllUser"resultType="list" resultMap="userInfoResultMap">
select * from userinfo
</select>
<selectid="get" parameterType="userinfo" resultType="com.hzs.pojo.UserInfo"resultMap="userInfoResultMap">
<![CDATA[
select * from userinfo where id =#{id}
]]>
</select>
<updateid="edit" parameterType="userinfo">
update userinfo set
username = #{username},
password = #{password}
where id = #{id}
</update>
<deleteid="remove"parameterType="userinfo">
delete from userinfo where id =#{id}
</delete>
<resultMaptype="com.hzs.pojo.UserInfo"id="userInfoResultMap">
<idproperty="id"column="id"/>
<resultproperty="username"column="username"/>
<resultproperty="password"column="password"/>
<resultproperty="ismanager"column="ismanager"/>
</resultMap>
</mapper>
UserInfoMapper.java
package com.mapper;
import java.util.List;
import com.hzs.pojo.UserInfo;
public interface UserInfoMapper {
publicvoid insertUser(UserInfo user);
public void edit(UserInfo user);
public void remove(UserInfo user);
public UserInfo get(UserInfo user);
public List getList(UserInfo user);
public List getAllUser();
}
Mybatis的配置文件:mybatis-config.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEconfiguration PUBLIC"-//mybatis.org//DTDConfig 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAliasalias="userinfo"type="com.hzs.pojo.UserInfo"/>
<typeAliasalias="user"type="com.hzs.pojo.User"/>
</typeAliases>
</configuration>
6.业务逻辑层
IUserInfoService接口
package com.hzs.service;
import java.util.List;
import com.hzs.pojo.UserInfo;
public interface IUserInfoService {
publicvoid insertUser(UserInfo user);
public void edit(UserInfo user);
public void remove(UserInfo user);
public UserInfo get(UserInfo user);
public List getList(UserInfo user);
public List getAllUser();
}
UserInfoService类
package com.hzs.service;
import java.util.Iterator;
import java.util.List;
importorg.springframework.beans.factory.annotation.Autowired;
import com.hzs.pojo.UserInfo;
import com.mapper.UserInfoMapper;
public class UserInfoServiceimplements IUserInfoService {
//注解
@Autowired
privateUserInfoMapper userInfoMapper;
@Override
public void insertUser(UserInfo user)
{
userInfoMapper.insertUser(user);
}
@Override
public void edit(UserInfo user) {
userInfoMapper.edit(user);
}
@Override
public void remove(UserInfo user) {
userInfoMapper.remove(user);
}
@Override
public UserInfo get(UserInfo user) {
UserInfouser1=userInfoMapper.get(user);
System.out.println(user1.getUsername());
return user1;
}
@Override
public List getList(UserInfo user) {
Listlist=userInfoMapper.getList(user);
for(Iteratoriter=list.iterator();iter.hasNext();)
{
UserInfouser1=(UserInfo)iter.next();
System.out.println(user1.getUsername()+",");
}
return list;
}
@Override
public List getAllUser() {
List list=userInfoMapper.getAllUser();
for(Iteratoriter=list.iterator();iter.hasNext();)
{
UserInfo user=(UserInfo)iter.next();
System.out.println(user.getUsername()+",");
}
return list;
}
}
7.Struts2的配置文件
Struts.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstruts PUBLIC
"-//Apache Software Foundation//DTD StrutsConfiguration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 将Action的创建交给spring来管理 -->
<constantname="struts.objectFactory"value="spring"/>
<!-- 更改struts2请求Action的后缀名,默认为action。若想去掉后缀,设为","即可 -->
<constantname="struts.action.extension"value=","></constant>
<packagename="abstract_struts"abstract="true"extends="struts-default"
namespace="/">
<!-- 公共东西可以放到这个抽象包下 -->
</package>
<!-- 包含的配置文件 -->
<includefile="/struts/struts-user.xml"></include>
</struts>
Struts-user.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstruts PUBLIC
"-//Apache Software Foundation//DTD StrutsConfiguration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<packagename="MyActions"extends="struts-default">
<actionname="*_*"class="userManageAction"method="{1}">
<resultname="success"type="redirect">/{2}.jsp</result>
</action>
</package>
</struts>
编写action类:
package com.hzs.action;
importorg.springframework.beans.factory.annotation.Autowired;
import com.hzs.pojo.UserInfo;
importcom.hzs.service.IUserInfoService;
importcom.hzs.service.UserInfoService;
public class UserManagerAction {
//注解userInfoService point:service注入声明必须为接口类
@Autowired
privateIUserInfoService userInfoService;
private UserInfo userInfo;
public UserInfo getUserInfo() {
return userInfo;
}
public void setUserInfo(UserInfo userInfo) {
this.userInfo = userInfo;
}
public String insertUser() {
userInfoService.insertUser(userInfo);
return "success";
}
public String editUser() {
userInfoService.edit(userInfo);
return "success";
}
public String removeUser() {
userInfoService.remove(userInfo);
return "success";
}
public String getUserById() {
userInfoService.get(userInfo);
return "success";
}
public String getListByName() {
userInfoService.getList(userInfo);
return "success";
}
public String getAllUser() {
userInfoService.getAllUser();
return "success";
}
}
8.UserInfoMapper和UserInfoService 分别在applicationContext-dao.xml和applicationContext-service.xml配置文件中注入,在UserManagerAction和UserInfoService类中通过注解使用
//注解userInfoService point:service注入声明必须为接口类
@Autowired
private IUserInfoServiceuserInfoService;
//注解
@Autowired
private UserInfoMapper userInfoMapper;
9.在web.xml 中配置struts和spring容器
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"id="WebApp_ID"version="3.1">
<display-name>SSMWeb</display-name>
<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>
<!-- 配置spring资源 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<!-- 配置spring -->
<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>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,/struts.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
10.Jsp文件:index.jsp
<%@ page contentType="text/html;charset=UTF-8"%>
<html>
<head>
<title></title>
<scripttype="text/javascript">
functioninsertUser()
{
var myform=document.forms[0];
myform.action="insertUser_success";
myform.method="post";
myform.submit();
}
functiongetUserByName()
{
var myform=document.forms[0];
myform.action="getListByName_success";
myform.method="post";
myform.submit();
}
functiongetAllUser()
{
var myform=document.forms[0];
myform.action="getAllUser_success";
myform.method="post";
myform.submit();
}
functioneditUser()
{
var myform=document.forms[0];
myform.action="editUser_success";
myform.method="post";
myform.submit();
}
functiongetUserById()
{
var myform=document.forms[0];
myform.action="getUserById_success";
myform.method="post";
myform.submit();
}
functionremoveUser()
{
var myform=document.forms[0];
myform.action="removeUser_success";
myform.method="post";
myform.submit();
}
</script>
</head>
<body>
<h1>用户管理</h1>
<hr>
<formname="myform">
id:<inputtype="text"name="userInfo.id"><br>
用户名:<inputtype="text"name="userInfo.username"><br>
密码:<inputtype="text"name="userInfo.password"> <br>
是否为管理员:<inputtype="text"name="userInfo.ismanager"><br>
<inputtype="button"name="btninsert"οnclick="insertUser()"value="增加"/>
<inputtype="button"name="btnedit"οnclick="editUser()"value="修改"/>
<inputtype="button"name="btnremove"οnclick="removeUser()"value="删除" /><br>
<inputtype="button"name="btnget"οnclick="getUserById()"value="按id查询" />
<inputtype="button"name="btngetlist"οnclick="getUserByName()"value="按名称查询" />
<inputtype="button"name="btngetall"οnclick="getAllUser()"value="查询全部" />
</form>
</body>
</html>