点击File -> New -> Project -> Maven -> 勾选 Create from archetype -> 选择 maven-archetype-webapp
对项目的pom.xml文件进行配置,在此文件中配置项目所需要的jar包。
4.0.0
com.zhongruan
Day3
1.0-SNAPSHOT
war
Day3 Maven Webapp
http://www.example.com
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
Day3
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
在resource包下新建Directory:“mapper”(用于存放xxxMapper.xml文件)和“spring”(用于存放spring-xxx.xml配置文件),导入配置文件:“jdbc.properties”(mysql数据库配置文件),”log4j.properties”(日志输出配置文件),”mybatis-config.xml”(mybatis框架配置文件)。
package com.zhongruan.bean;
//实体类,映射数据库
public class UserInfo {
//1.对属性的封装
private int id;
private String username;
private String password;
//2.getter,setter
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;
}
//3.构造方法
public UserInfo(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public UserInfo() {
}
//4.toString
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
import com.zhongruan.bean.UserInfo;
import javax.xml.registry.infomodel.User;
import java.util.List;
public interface UserDao {
//查询全部
List findAllUser();
//更新前的查询
UserInfo findUserById(int id);
//更新
void updUserById(UserInfo userInfo);
}
update tb_user set username=#{username},password=#{password} where id=#{id}
import com.zhongruan.bean.UserInfo;
import javax.xml.registry.infomodel.User;
import java.util.List;
public interface UserInfoService {
//查询全部
List findAllUser();
//更新前的查询
UserInfo findUserById(int id);
//更新
void updUserById(UserInfo userInfo);
}
package com.zhongruan.service.impl;
import com.zhongruan.bean.UserInfo;
import com.zhongruan.dao.UserDao;
import com.zhongruan.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.xml.registry.infomodel.User;
import java.util.List;
@Service
public class UserInfoServiceImpl implements UserInfoService {
@Autowired
private UserDao userDao;
@Override
public List findAllUser() {
return userDao.findAllUser();
}
@Override
public UserInfo findUserById(int id) {
return userDao.findUserById(id);
}
@Override
public void updUserById(UserInfo userInfo) {
userDao.updUserById(userInfo);
}
}
package com.zhongruan.controller;
import com.zhongruan.bean.UserInfo;
import com.zhongruan.service.UserInfoService;
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 javax.xml.registry.infomodel.User;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserInfoController {
@Autowired
private UserInfoService userInfoService;
@RequestMapping("findAllUser.do")
public ModelAndView findAllUser(){
//1.调用service方法查询并得到结果
List userInfoList=userInfoService.findAllUser();
//2.实例化ModelView
ModelAndView mv=new ModelAndView();
mv.addObject("userInfoList",userInfoList);
mv.setViewName("user-list");
return mv;
}
//修改前的查询
@RequestMapping("/findUserById.do")
public ModelAndView findUserById(int id){
UserInfo userInfo = userInfoService.findUserById(id);
System.out.println("得到的Id"+id);
ModelAndView mv=new ModelAndView();
mv.addObject("userInfo",userInfo);
mv.setViewName("user-update");
return mv;
}
//更新
@RequestMapping("/updUserById.do")
public String updUserById(UserInfo userInfo){
userInfoService.updUserById(userInfo);
return "redirect:findAllUser.do";
}
}
页面用老师给的素材