1、新建Maven项目
打开IDEA点击File->New->Project,选择Maven项,勾选Create from archetype,再选择右侧中的maven-archetype-webapp
2.目录结构
3.代码实现
UserController
package com.zr.controller;
import com.zr.bean.User;
import com.zr.service.IUserService;
import org.mybatis.spring.SqlSessionFactoryBean;
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(User user){
boolean flag = userService.login(user.getUsername(),user.getPassword());
ModelAndView modelAndView=new ModelAndView();
if(flag){
modelAndView.setViewName("main");
}else {
modelAndView.setViewName("../failer");
}
return modelAndView;
}
@RequestMapping("findAll.do")
public ModelAndView findAll(){
List users = userService.findAll();
ModelAndView mv=new ModelAndView();
mv.setViewName("user-list");
mv.addObject("users",users);
return mv;
}
@RequestMapping("deleteById.do")
public String delete(int id){
userService.deleteById(id);
return "redirect:findAll.do";
}
@RequestMapping("add.do")
public String add(User user){
userService.add(user);
return "redirect:findAll.do";
}
@RequestMapping("toUpdate.do")
public ModelAndView toUpdate(int id){
User user=userService.selectUserById(id);
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("user",user);
modelAndView.setViewName("user-update");
return modelAndView;
}
@RequestMapping("update.do")
public String update(User user){
userService.update(user);
return "redirect:findAll.do";
}
}
IUserDao
package com.zr.dao;
import com.zr.bean.User;
import java.util.List;
public interface IUserDao {
User findUserByUsername(String username);
List findAll();
void deleteById(int id);
void add(User user);
User selectUserById(int id);
void update(User user);
}
UserServiceImpl
package com.zr.service.impl;
import com.zr.bean.User;
import com.zr.dao.IUserDao;
import com.zr.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public boolean login(String username, String password) {
User user = userDao.findUserByUsername(username);
if(user!=null&&user.getPassword().equals(password)){
return true;
}
return false;
}
@Override
public List findAll() {
return userDao.findAll();
}
@Override
public void deleteById(int id) {
userDao.deleteById(id);
}
@Override
public void add(User user) {
userDao.add(user);
}
@Override
public User selectUserById(int id) {
return userDao.selectUserById(id);
}
@Override
public void update(User user) {
userDao.update(user);
}
}
IUserService
package com.zr.service;
import com.zr.bean.User;
import java.util.List;
public interface IUserService {
boolean login(String username,String password);
List findAll();
void deleteById(int id);
void add(User user);
User selectUserById(int id);
void update(User user);
}
UserMapper.xml
delete from tb_user where id =#{id}
insert into tb_user(username,password) values (#{username},#{password})
update tb_user set username=#{username},password=#{password} where id =#{id}
applicationContext.xml
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/whlg?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
pom.xml
4.0.0
org.example
ssm3
1.0-SNAPSHOT
war
ssm3 Maven Webapp
http://www.example.com
UTF-8
1.8
1.8
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
8.0.20
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
ssm3
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2