您的位置:首页 > 编程语言

实训第五天之ssh框架后台代码实现

2017-12-15 15:36 323 查看
实训第五天,学习了ssh框架下的对信息的增删改查和用户的登录。

1、在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Model 返回给对应的View 进行展示。在SpringMVC 中提供了一个非常简便的定义Controller 的方法,你无需继承特定的类或实现特定的接口,只需使用@Controller 标记一个类是Controller ,然后使用@RequestMapping 和@RequestParam 等一些注解用以定义URL 请求和Controller 方法之间的映射,这样的Controller 就能被外界访问到。此外Controller 不会直接依赖于HttpServletRequest 和HttpServletResponse 等HttpServlet 对象,它们可以通过Controller 的方法参数灵活的获取到。

Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象。分发处理器将会扫描使用了该注解的类的方法,并检测该方法 是否使用了@RequestMapping 注解。@Controller只是定义了一个控制器类,而使用@RequestMapping 注解的方法才是真正处理请求的处理器

2、Controller中 使用@RequestMapping 可以设置访问的路径和方式等属性,通过查看相关文档,RequestMapping中共有六个属性:value, method,consumes,produces,params,headers:

1)value:   指定请求的实际地址,指定的地址可以是URI Template 模式(后面将会说明);
2)method:  指定请求的method类型, GET、POST、PUT、DELETE等;;
3)consumes:指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
4)produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
5)params:  指定request中必须包含某些参数值是,才让该方法处理。
6)headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。


例如:@RequestMapping(value = “queryUser”, method = { RequestMethod.GET })

一个Controller类的源码:

package com.iss.controller;

import java.util.List;

import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.iss.entity.User;
import com.iss.service.UserService;
//指定了一个控制器类
@Controller
//指定类的访问路径为/user
@RequestMapping("/user")
public class UserController {
/**
Spring 2.5 引入了 @Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法。*/
@Autowired
private UserService userService;

//定义queryUser()方法的相对访问路径为queryUser  当在请求的queryUser的时候需要通过/user/queryUser进行访问。/user为类的路径
@RequestMapping(value = "queryUser", method = { RequestMethod.GET })
public ModelAndView queryUser() throws Exception {
List<User> userList = userService.findAll();
/**
ModelAndView类别就如其名称所示,是代表了MVC Web程序中Model与View的对象,
mv.addObject("userList", userList);可以像request一样将数据一起传入跳转的页面中
mv.setViewName("user/list");添加一个响应jsp界面,在return ModelAndView的对象的时候显示
*/
ModelAndView mv = new ModelAndView();
mv.addObject("userList", userList);
mv.setViewName("user/list");
return mv;
}

// 编辑
@RequestMapping(value = "editUser", method = { RequestMethod.POST })
public String edit(User user) throws Exception {

userService.modify(user);
//重定向,将页面重定向到queryUser.action请求
return "redirect:/user/queryUser.action";

}

// 编辑ui
@RequestMapping("edit")
public ModelAndView editUi(Integer id) throws Exception {
User user = userService.findById(id);
ModelAndView mv = new ModelAndView();
mv.addObject("user", user);
mv.setViewName("user/edit2");
return mv;
}

@RequestMapping(value = "login",method= {RequestMethod.POST})
public String login(@RequestParam("account")String account,@RequestParam("pwd")String pwd,HttpSession session,Model model) throws Exception{
//HttpSession session为了将用户登陆的信息储存在session中,使用session.setAttribute()方法
//Model  使用Model完成对账号密码错误的回显功能,Model对象的使用方法和session对象很相似
List<User> users = userService.findByAccountAndPwd(account, pwd);
if(users != null && users.size() ==1) {
session.setAttribute("session_user", users.get(0));
return "redirect:/user/queryUser.action";
}else {

model.addAttribute("error", "用户名或者密码错误");
model.addAttribute("account", account);
return "user/login";
}

}

}


3、mybatis Mapper的配置:

UserMapper接口的定义:

@Mapper注解的componentModel属性:

componentModel属性用于指定自动生成的接口实现类的组件类型。这个属性支持四个值:

default: 这是默认的情况,mapstruct不使用任何组件类型, 可以通过Mappers.getMapper(Class)方式获取自动生成的实例对象。

cdi: the generated mapper is an application-scoped CDI bean and can be retrieved via @Inject

spring: 生成的实现类上面会自动添加一个@Component注解,可以通过Spring的 @Autowired方式进行注入

jsr330: 生成的实现类上会添加@javax.inject.Named 和@Singleton注解,可以通过 @Inject注解获取。


package com.iss.mapper;

import java.util.List;

import org.mybatis.spring.annotation.Mapper;

import com.iss.entity.User;

@Mapper
public interface UserMapper {

List<User> selectAll();

void insert(User u);

void update(User u);

void deleteById(Integer id);

User selectById(Integer id);

List<User> selectByAccountAndPwd(String account, String pwd);

}


4、UserMapper.xml:

在mybatis的配置文件中:

使用< resultMap type=”” id=”“>设置结果集的类型和id 例如:

<resultMap type="com.iss.entity.User" id="BaseResultMap">
<id column="id" property="id" />
<result column="account" property="account" />
<result column="pwd" property="pwd" />
<result column="phoneNumber" property="phoneNumber" />
<result column="email" property="email" />
</resultMap>


使用< sql >可以标识常用的属性例如:

<sql id="Base_Column_List">
id,account,pwd,phoneNumber,email
</sql>


使用select、insert、delete、update等标签配置对应的行为 属性parameterType为传入参数的类型,resultMap为结果集的id,id为在UserMapper中定义的函数名字,

例如:

<select id="selectById" parameterType="Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"></include>
from t_user where
id = #{id}
</select>


注意: 当传进的参数有多个的时候不能通过id = #{id} 的形式进行取值,应用序号的形式进行赋值:account = #{0} and pwd = #{1}

下面为完整的配置源码:

<?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.iss.mapper.UserMapper">
<resultMap type="com.iss.entity.User" id="BaseResultMap">
<id column="id" property="id" />
<result column="account" property="account" />
<result column="pwd" property="pwd" />
<result column="phoneNumber" property="phoneNumber" />
<result column="email" property="email" />
</resultMap>
<sql id="Base_Column_List"> id,account,pwd,phoneNumber,email </sql>
<select id="selectAll" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"></include>
from t_user
</select>
<insert id="insert" parameterType="com.iss.entity.User">
insert into t_user
(account,pwd,phoneNumber,email)
values(#{account},#{pwd},#{phoneNumber},#{email})
</insert>
<update id="update" parameterType="com.iss.entity.User">
update t_user set account =
#{account},pwd=#{pwd},phoneNumber=#{phoneNumber},email=#{email} where
id =#{id}
</update>
<!-- 函数名 类型 -->
<delete id="deleteById" parameterType="Integer">
delete from t_user where
id = #{id}
</delete>
<select id="selectById" parameterType="Integer" resultMap="BaseResultMap"> select <include refid="Base_Column_List"></include> from t_user where id = #{id} </select>
<select id="selectByAccountAndPwd" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"></include>
from t_user
where account = #{0} and pwd = #{1}
</select>

</mapper>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息