SSM框架实现项目的增删改查
一、基本的程序框架包结构
上篇博客有查询部分,所以这次博客接着完成之后的
包结构如下:
二、相关代码
UserInfo.java,将user封装起来
package com.zhongruan.bean;
public class UserInfo {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
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;
}
@Override
public String toString() {
return "UserInfo{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
UserController.java,控制器,接受请求并处理
package com.zhongruan.controller;
import com.zhongruan.bean.UserInfo;
import com.zhongruan.service.IUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/findAll.do")
public ModelAndView findAll()
{
List<UserInfo> all =userService.findAll();
ModelAndView mv=new ModelAndView();
mv.addObject("userinfos",all);
mv.setViewName("allUser");
return mv;
}
@RequestMapping("/delete.do")
public ModelAndView delete(int id){
userService.delete(id);
ModelAndView mv=new ModelAndView();
mv= findAll();
return mv;
}
@RequestMapping("/toAddUser.do")
public ModelAndView addUser(){
ModelAndView mv=new ModelAndView();
mv.setViewName("addUser");
return mv;
}
@RequestMapping("/save.do")
public ModelAndView insert(@Param("userinfo")UserInfo userinfo){
userService.insert(userinfo);
List<UserInfo> all =userService.findAll();
ModelAndView mv=new ModelAndView();
mv.addObject("userinfos",all);
mv.setViewName("allUser");
return mv;
}
@RequestMapping("/toUpdate.do")
public ModelAndView toupdate(@Param("id")int id)
{
UserInfo userinfo=new UserInfo();
userinfo=userService.find(id);
ModelAndView mv=new ModelAndView();
mv.addObject("userInfo",userinfo);
mv.setViewName("updateUser");
return mv;
}
@RequestMapping("/update.do")
public ModelAndView update(@Param("userinfo")UserInfo userinfo)
{
userService.update(userinfo);
ModelAndView mv=new ModelAndView();
mv= findAll();
return mv;
}
@RequestMapping("/login.do")
public String login(String username,String password){
UserInfo userInfo=userService.login(username);
if(userInfo !=null){
if(userInfo.getPassword().equals(password)){
System.out.println("登陆成功");
return "redirect:/success.jsp";
}else {
System.out.println("密码错误");
return "redirect:/fail.jsp";
}
}else {
System.out.println("用户名不存在");
return "redirect:/fail.jsp";
}
}
}
IUserDao.java ,封装数据库操作
package com.zhongruan.dao;
import com.zhongruan.bean.UserInfo;
import javax.xml.registry.infomodel.User;
import java.util.List;
public interface IUserDao {
List<UserInfo> findAll();
void insert(UserInfo userinfo);
void delete(int id);
void update(UserInfo userinfo);
UserInfo find(int id);
UserInfo login(String username);
}
IUserService ,service类
package com.zhongruan.service;
import com.zhongruan.bean.UserInfo;
import java.util.List;
public interface IUserService {
public List<UserInfo> findAll();
void insert(UserInfo userinfo);
void delete(int id);
UserInfo find(int id);
void update(UserInfo userinfo);
UserInfo login(String username);
}
UserService.java ,实现Service类接口
package com.zhongruan.service.impl;
import com.zhongruan.bean.UserInfo;
import com.zhongruan.dao.IUserDao;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public List<UserInfo> findAll() {
return userDao.findAll();
}
@Override
public void insert(UserInfo userinfo) {
userDao.insert(userinfo);
}
@Override
public void delete(int id) {
userDao.delete(id);
}
@Override
public UserInfo find(int id)
{
return userDao.find(id);
}
@Override
public void update(UserInfo userinfo) {
userDao.update(userinfo);
}
@Override
public UserInfo login(String username) {
return userDao.login(username);
}
}``
UserMapper.xml,实现Dao
<?xml version="1.0" encoding="UTF-8" ?>
<select id="findAll" resultType="com.zhongruan.bean.UserInfo">
select * from userinfo
<insert id="insert" parameterType="com.zhongruan.bean.UserInfo">
insert into userinfo values (#{id},#{username},#{password})
</insert>
<delete id="delete" parameterType="com.zhongruan.bean.UserInfo">
delete from userinfo
where id = #{id}
</delete>
<select id="find" parameterType="int" resultType="com.zhongruan.bean.UserInfo">
select * from userinfo where id=#{id}
</select>
<update id="update" parameterType="com.zhongruan.bean.UserInfo" >
update userinfo set username=#{username},password=#{password} where id=#{id}
</update>
<select id="login" parameterType="string" resultType="com.zhongruan.bean.UserInfo">
select * from userinfo where username=#{username}
```
applicationContext.xml
<?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: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-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 1.配置数据库相关参数properties的属性:${url} -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 2.配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="2"/>
</bean>
<!-- 3.配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 扫描bean包 使用别名 -->
<property name="typeAliasesPackage" value="com.zhongruan.bean"></property>
<!--配置加载映射文件 UserMapper.xml-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 自动生成dao,m