Struts2+spring3+mybatis3整合注解开发

时间:2022-04-03 04:59:45


Struts2+spring3+mybatis3整合注解开发

这是myeclipse工程目录结构

接下来先web.xml

  
  
  
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

contextConfigLocation
classpath:applicationContext.xml


org.springframework.web.context.ContextLoaderListener



index.jsp



struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter



struts2
/*



applicationcontext.xml

  
  
  
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-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">











class="org.springframework.jdbc.datasource.DriverManagerDataSource">




















class="org.springframework.jdbc.datasource.DataSourceTransactionManager">



















mybatis-config,xml

  
  
  
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">








struts.xml
   
   
   


















下面是代码
package com.action;
import org.springframework.beans.factory.annotation.Autowired;
import com.entity.SnUser;
import com.opensymphony.xwork2.ActionSupport;
import com.service.SnUserService;
import org.apache.struts2.convention.annotation.Action;  
import org.apache.struts2.convention.annotation.Result;  
@Action(value = "snUserAction", results = {  
        @Result(name = "saveok", location = "/suning_login.html"),
        @Result(name = "loginok", location= "/suning_main.html"),
        @Result(name = "loginno", params="shibai", location= "/suning_login.html")})  
public class SnUserAction extends ActionSupport{
private SnUserService snUserService;
@Autowired
public void setSnUserService(SnUserService snUserService) {
    this.snUserService = snUserService;
}
private SnUser snUser;
public SnUser getSnUser() {
    return snUser;
}
public void setSnUser(SnUser snUser) {
    this.snUser = snUser;
}
public String save(){
        snUserService.save(snUser);    
    return "saveok";
}
public String login(){
boolean flag=snUserService.login(snUser);
if(flag){
    return "loginok";
}else {
    return "loginno";
}
}
}


package com.service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dao.imp.SnUserDAO;
import com.entity.SnUser;
@Service
public class SnUserService {
private SnUserDAO snUserDAO;
@Autowired
public void setSnUserDAO(SnUserDAO snUserDAO) {
    this.snUserDAO = snUserDAO;
}
//注册
public void save(SnUser snUser){
    snUserDAO.save(snUser);    
}
//登录
public boolean login(SnUser snUser){
    SnUser user=snUserDAO.getUserByName(snUser.getUsername());
    if(user!=null){
        if(snUser.getPassword().equals(user.getPassword())){
            return true;
        }else {
            return false;
        }
    }else {
        return false;
    }
    
}
}

package com.entity;

import java.io.Serializable;

public class SnUser implements Serializable{
private Integer uid;
private String username;
private String password;
public Integer getUid() {
    return uid;
}
public void setUid(Integer uid) {
    this.uid = uid;
}
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;
}

}

<?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">
<!--等价于dao接口的实现 namespace必须和接口的类路径一样 -->
<mapper namespace="com.dao.imp.SnUserDAO">
    <resultMap type="com.entity.SnUser" id="SnUserResultMap">
        <id property="uid" column="uid" />
        <result property="username" column="username" />
        <result property="password" column="password" />
    </resultMap>
    <!-- id必须和接口中的方法名一样 -->
    <insert id="save" parameterType="SnUser">
        <![CDATA[
             INSERT INTO sn_user(username,password) VALUES(#{username},#{password})
        ]]>
    </insert>
    <update id="update" parameterType="SnUser">
        <![CDATA[
             UPDATE sn_user SET username=#{username},password=#{password} WHERE uid=#{uid}
        ]]>
    </update>
    <delete id="delete" parameterType="int">
        <![CDATA[
             DELETE FROM sn_user WHERE uid=#{uid}
        ]]>
    </delete>
    <select id="getUserById" parameterType="int" resultType="SnUser"
        resultMap="SnUserResultMap">
        <![CDATA[
             SELECT * FROM sn_user WHERE uid=#{uid}
        ]]>
    </select>
        <select id="getUserByName" parameterType="java.lang.String" resultType="SnUser"
        resultMap="SnUserResultMap">
        <![CDATA[
             SELECT * FROM sn_user WHERE username=#{username};
        ]]>
    </select>
    <select id="getUserList" resultType="SnUser" resultMap="SnUserResultMap">
        <![CDATA[
             SELECT * FROM sn_user
        ]]>
    </select>
</mapper>

package com.dao.imp;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.entity.SnUser;
@Repository("snUserDAO")
public interface SnUserDAO {
public void save(SnUser snUser);
public void update(SnUser snUser);
public void delete(int uid);
public SnUser getUserById(int uid);
public List<SnUser> getUserList();
public SnUser getUserByName(String username);
}
下面是新增页面的jsp
 <s:form action="snUserAction!save" method="post">
  <s:textfield label="USERNAME" name="snUser.username"/>
  <s:password label="PASSWORD" name="snUser.password"/>
    <s:submit value="新增"/>
    </s:form>
这些配好了如果想要ssm整合的lib请访问我的资源主页找http://download.csdn.net/user/u010982467