上篇博客中实现了数据库的增删查改,今天用另一种包结构实现增删查改以及用户登录功能。
1.开发工具
IntelliJ IDEA 2018
JDK1.8
tomcat 7.0.79
Mysql 5.0
Maven 3.6.0
2.工程结构
备注:在总项目下新建四个module
在新建ssm_bean\ssm_dao\ssm_service时,不勾选create from archetepe和下面的webapp;
在新建ssm_web时,勾选和上篇博客中相似的create from archetepe和下面的webapp(建立目录和包结构如果还有问题请参见上篇博客)
3.配置文件
(1)总工程项目文件下的pom.xml(不是每个module下的pom.xml)
4.0.0
com.zhongruan
ssm_parent
pom
1.0-SNAPSHOT
ssm_bean
ssm_dao
ssm_service
ssm_web
UTF-8
1.7
1.7
5.0.2.RELEASE
3.2.6
1.7.7
1.2.17
0.9.5.2
1.1.2
org.springframework
spring-core
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-oxm
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-test
${spring.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.2.2
javax
javaee-api
7.0
mysql
mysql-connector-java
5.1.30
commons-dbcp
commons-dbcp
1.2.2
jstl
jstl
1.2
log4j
log4j
${log4j.version}
com.mchange
c3p0
${c3p0.version}
taglibs
standard
${taglibs.version}
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
javax.servlet
javax.servlet-api
3.1.0
provided
javax.servlet.jsp
javax.servlet.jsp-api
2.3.1
provided
(2)ssm_web下的resources下的applicationContext.xml
(3)在ssm_web->resources下db.properties文件中配置mysql数据库(包括数据库名和密码)
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/zyt?useSSL=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
(4)ssm_web->resources下的spring-mvc.xml
(5)ssm_web->webapp->WEB-INF下的web.xml
contextConfigLocation
classpath*:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
org.springframework.web.context.request.RequestContextListener
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
dispatcherServlet
*.do
3.各包下代码(基本同上篇博客)
(1)在ssm_dao->resources->mapper包下新建 userMapper.xml
insert into userinfo(id,username,password) values (#{id},#{username},#{password})
delete from userinfo where id=#{id}
update userinfo
set username = #{username},password = #{password}
where id = #{id}
(2)实体类(bean包下UserInfo.java)
package com.zhongruan.bean;
public class UserInfo {
private int id;
private String password;
private String username;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "com.zhongruan.bean.UserInfo{" +
"id=" + id +
", password='" + password + '\'' +
", username='" + username + '\'' +
'}';
}
(3)dao包下接口IUserDao.java
package com.zhongruan.dao;
import com.zhongruan.bean.UserInfo;
import java.util.List;
public interface IUserDao {
public UserInfo doLogin(UserInfo userInfo);
public List findall();
public int adduser(UserInfo userinfo);
public int deleteuserbyid(int id);
public int updateuser(UserInfo userinfo);
public UserInfo getuser(int id);
}
(4)service中的接口IUserService.java
package com.zhongruan.service;
import com.zhongruan.bean.UserInfo;
import java.util.List;
public interface IUserService {
public Boolean doLogin(UserInfo userInfo);
List findall();
int adduser(UserInfo userinfo);
int deleteuserbyid(int id);
int updateuser(UserInfo userinfo);
UserInfo getuser(int id);
(5)service包下的实现包impl下的UserService.java
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 Boolean doLogin(UserInfo userInfo) {
UserInfo userInfo1=userDao.doLogin(userInfo);
if(null!=userInfo1){
return true;
}else {
return false;
}
}
@Override
public List findall() {
return userDao.findall();
}
@Override
public int adduser(UserInfo userinfo) {
return userDao.adduser(userinfo);
}
@Override
public int deleteuserbyid(int id) { return userDao.deleteuserbyid(id); }
@Override
public int updateuser(UserInfo userinfo){return userDao.updateuser(userinfo);}
@Override
public UserInfo getuser(int id) {
return userDao.getuser(id);
}
}
(6)ssm_web下的controller包下的UserController.java
package com.zhongruan.controller;
import com.zhongruan.bean.UserInfo;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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("/login.do")
public ModelAndView login(UserInfo userInfo){
ModelAndView mv=new ModelAndView();
Boolean flag=userService.doLogin(userInfo);
if(flag){
mv.setViewName("main");
}else{
mv.setViewName("../failer");
}
return mv;
}
@RequestMapping("/findAll.do")
public ModelAndView findall() {
List all = userService.findall();
ModelAndView mv=new ModelAndView();
mv.addObject("userInfos", all);
mv.setViewName("/user-list");//跳转到allUser
return mv;
}
@RequestMapping("/delete.do")
public String deleteuser(int id){
userService.deleteuserbyid(id);
return "redirect:findAll.do";//重定向
}
@RequestMapping("/save.do")
public String adduser(UserInfo userinfo){
userService.adduser(userinfo);
return "redirect:findAll.do";//重定向
}
@RequestMapping("/toUpdate.do")
public ModelAndView toUpdateUser(int id){
UserInfo userInfo=userService.getuser(id);
ModelAndView mv=new ModelAndView();
mv.setViewName("/user-update");
mv.addObject("userInfo",userInfo);
return mv;
}
@RequestMapping("/update.do")
public String updateuser(UserInfo userinfo){
userService.updateuser(userinfo);
return "redirect:findAll.do";//重定向
}
}
4.页面设计
链接:https://pan.baidu.com/s/1qByGvlIqtIdYmY1gXuDImw
提取码:z6fo
复制这段内容后打开百度网盘手机App,操作更方便哦
以上是页面各文件代码,将css、plugins、img、pages这四个文件复制粘贴到ssm_web—>webapp下;将login.jsp\failer.jsp\index.jsp这三个文件复制粘贴到web-inf下。
这三个jsp文件下加入相应的跳转链接。
5.运行效果图