第一篇 Springboot + Web MVC + MyBatis + 简单UI + Thymeleaf实现
源码链接:https://pan.baidu.com/s/1-LtF56dnCM277v5lILRM7g
提取码:c374
第二篇 Springboot mybatis generate根据数据库表自动生成实体类、Mapper和Mapper.xml
第一个博文没什么废话直接上图和代码
点击【File】- 【New】-【Project】新建项目,
点击【Finish】完成项目的创建,目录结构如下
连接数据库还需要在pom.xml文件中增加一个依赖spring-boot-starter-jdbc,同时将mysql-connector-java依赖的scope属性修改成version版本不修改可能会出现数据库连接不上,另外增增加了依赖项thymeleaf是个模板框架前端使用的。
maven工程在默认情况下src/main/java目录下的mapper文件是不发布到target目录下的,所以我们还要在pom.xml增加下面配置
pom.xml文件依赖部分和resources部分代码如下:
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.1
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-thymeleaf
2.1.4.RELEASE
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
src/main/resources
**/*.*
true
src/main/java
**/*.properties
**/*.xml
false
org.springframework.boot
spring-boot-maven-plugin
下面我们来修改程序启动项配置和数据库连接配置:
将application.properties文件重命名为application.yml,鼠标右键-【Refactor】-【Rname】,之后编辑application.yml文件
在resources目录下新建mybatis-config.xml文件,代码如下图片部分代码编辑器会自动隐藏只能截图:
配置项都已完成,下面开始代码部分目录结构如下图新增Controller、Service、Mapper、Model:
选中big.strong.disheng右键【New】-【Package】输入big.strong.disheng.user.model创建model包,在model右键【New】-【Java Class】添加实体类User,添加4个属性
代码生成getter和setter的快捷键是alt+Insert:
package big.strong.disheng.user.model;
public class User {
private Long userId;
private String username;
private String password;
private Integer age;
public Long getUserId() { return userId; }
public void setUserId(Long userId) { this.userId = userId; }
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;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
在dao包添加UserMapper类
代码:
package big.strong.disheng.user.dao;
import big.strong.disheng.user.model.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
int deleteByPrimaryKey(Long userid);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Long userid);
List selectAll();
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
dao包下的UserMapper.xml文件内容(增删改查语句)编辑器将其大部分内容忽略显示异常截取部分截图供参考,需要完整代码可到最下方有连接去下载
service包中的UserService接口文件很简单,是User的查询和修改两个接口代码
package big.strong.disheng.user.service;
import big.strong.disheng.user.model.User;
import java.util.List;
public interface UserService {
List selectAll();
int updateByPrimaryKeySelective(User record);
}
UserServiceImpl类
UserServiceImpl代码:
package big.strong.disheng.user.service.Impl;
import big.strong.disheng.user.dao.UserMapper;
import big.strong.disheng.user.model.User;
import big.strong.disheng.user.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public List selectAll() { return userMapper.selectAll(); }
@Override
public int updateByPrimaryKeySelective(User record) {
return userMapper.updateByPrimaryKeySelective(record);
}
}
控制类UserController如下图:
上面修改User的请求一定注意@ResponseBody和@RequestBody别漏掉,否则后台会无法响应前端的请求
UserController代码:
package big.strong.disheng.user.controller;
import big.strong.disheng.user.model.User;
import big.strong.disheng.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/list")
public ModelAndView list(){
List users=userService.selectAll();
ModelMap model = new ModelMap();
model.addAttribute("users",users);
return new ModelAndView("userlist",model);
}
@RequestMapping(value = "/update", method = { RequestMethod.POST })
@ResponseBody
public Map update(@RequestBody User user){
Map result=new HashMap<>(1);
int i = userService.updateByPrimaryKeySelective(user);
if(i > 0){ result.put("success","ok"); }
else{ result.put("success","更新失败"); }
return result;
}
最后上userlist.html代码中thymeleaf绑定数据如下
userlist.html代码编辑器无法显示,需要完整代码可到最下方有连接去下载
到目前为止代码全部完成,点击菜单栏【Build】-【Rebuild Ojbect】编译项目,编译完成后点击菜单栏【Run】-【Debug ‘DishengApplication’】项目运行(DishengApplication是项目名称),打开浏览器输入http://localhost:8001/user/list显示下面画面,端口号是在application.yml中设置的,前提数据库中有数据
点击【编辑】可以修改用户名称
第二篇 Springboot mybatis generate根据数据库表自动生成实体类、Mapper和Mapper.xml
源码链接:https://pan.baidu.com/s/1-LtF56dnCM277v5lILRM7g
提取码:c374