概述
这是myeclipse工程目录结构
接下来先web.xml
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
index.jsp
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
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";
}
}
}
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
最后
以上就是小巧奇迹为你收集整理的Struts2+spring3+mybatis3整合注解开发的全部内容,希望文章能够帮你解决Struts2+spring3+mybatis3整合注解开发所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复