maven+spring boot+mybatis实现简单的CURD操作

使用spring boot进行简单的CURD操作的练习(没有添加事务相关的代码),页面使用thymeleaf,这篇文章只是我学习的一个记录,代码很不完整,在这里只是记录一下学习过程。

目录

一、项目的整体结构

二、配置文件

1、pom.xml

2、application.properties

三、数据库SQL

四、实体类

五、controller、service、dao

1、controller

2、service

a、interface

b、class

3、dao

a、interface

b、xml

六、启动类

七、页面

1、add.html

2、modify.html

3、userinfo.html

4、fail.html


一、项目的整体结构

maven+spring boot+mybatis实现简单的CURD操作_第1张图片

先说一下配置文件

二、配置文件

1、pom.xml


	4.0.0

	com.llangzh
	myspringbootcurd
	1.0.0

	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.4.RELEASE
	

	
		3.0.11.RELEASE
		2.4.1
	

	
		
		
			org.springframework.boot
			spring-boot-starter-web
		

		
		
			org.springframework.boot
			spring-boot-starter-thymeleaf
		

		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			2.0.1
		
	
		
		
		    mysql
		    mysql-connector-java
		
				
		
		
		    com.alibaba
		    druid
		    1.1.16
		
	

2、application.properties

# 连接池配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/myspringboot?serverTimezone=GMT%2B8&userUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456

#类别名
mybatis.type-aliases-package=com.llangzh.bean

三、数据库SQL

SET FOREIGN_KEY_CHECKS=0;

DROP TABLE IF EXISTS `user`;

CREATE TABLE `user` (
  `uid` int(5) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) COLLATE utf8_croatian_ci NOT NULL,
  `password` varchar(20) CHARACTER SET utf8 COLLATE utf8_croatian_ci NOT NULL,
  `age` int(3) DEFAULT NULL,
  `address` varchar(40) COLLATE utf8_croatian_ci DEFAULT NULL,
  `email` varchar(40) COLLATE utf8_croatian_ci NOT NULL,
  PRIMARY KEY (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_croatian_ci;

四、实体类

使用hibernate validator注解验证

package com.llangzh.bean;

import javax.validation.constraints.Email;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;

import org.hibernate.validator.constraints.Length;

public class User {
	private Integer uid;
	@NotBlank
	private String username;
	@NotBlank
	private String password;
	@Min(1)
	@Max(150)
	private Integer age;
	@Length(max = 40)
	private String address;
	@Email(regexp = "/^(\\w)+(\\.\\w+)*@(\\w)+((\\.\\w{2,3}){1,3})$/")
	private String email;

	public Integer getUid() {
		return uid;
	}
	public void setUid(Integer uid) {
		this.uid = uid;
	}
	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;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
}

五、controller、service、dao

1、controller

package com.llangzh.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.llangzh.bean.User;
import com.llangzh.service.IUserService;

@Controller
@RequestMapping("/userController")
public class UserController {

	@Autowired
	private IUserService userService;

	@RequestMapping("/addPage")
	public String regesterPage() {
		return "add.html";
	}

	/**
	 * 添加user,添加成功重定向到用户信息页面
	 * @param user
	 * @param model
	 * @param attributes
	 * @return
	 */
	@RequestMapping("/addUser")
	public String addUser(User user, Model model, RedirectAttributes attributes) {
		if (userService.addUser(user)) {
			attributes.addAttribute("uid", user.getUid());
			return "redirect:/userController/findUserByUid";
		}
		return "fail.html";
	}

	/**
	 * 删除用户
	 * @param uid
	 * @return
	 */
	@RequestMapping("/moveUser")
	public String moveUser(Integer uid) {
		if (userService.findUserByUid(uid) != null) {
			userService.moveUserByUid(uid);
			return "redirect:/userController/findAllUser";
		}
		return "fail.html";
	}

	/**
	 * 查找先要修改的用户,查到跳转到修改页面
	 * @param uid
	 * @param model
	 * @return
	 */
	@RequestMapping("/modifyUser")
	public String modifyUser(Integer uid, Model model) {
		User user = userService.findUserByUid(uid);
		if (user != null) {
			model.addAttribute("user", user);
			return "modify.html";
		}
		return "fail.html";
	}
	
	/**
	 * 修改用户,跳转到用户信息页面
	 * @param user
	 * @return
	 */
	@RequestMapping("/modifyUserByUid")
	public String modifyUserByUid(User user) {
		userService.modifyUserByUid(user);
		return "redirect:/userController/findUserByUid?uid=" + user.getUid();
	}

	/**
	 * 查所有用户
	 * @param model
	 * @return
	 */
	@RequestMapping("/findAllUser")
	public String findAllUser(Model model) {
		List users = userService.findAllUser();
		model.addAttribute("users", users);
		return "userinfo.html";
	}

	/**
	 * 通过ID查user,显示到用户信息页面
	 * @param uid
	 * @param model
	 * @return
	 */
	@RequestMapping("/findUserByUid")
	public String findUserByUid(Integer uid, Model model) {
		User user = userService.findUserByUid(uid);
		if (user != null) {
			List users = new ArrayList();
			users.add(user);
			model.addAttribute("users", users);
			return "userinfo.html";
		}
		return "fail.html";
	}
}

2、service

a、interface

package com.llangzh.service;

import java.util.List;

import com.llangzh.bean.User;

public interface IUserService {

	boolean addUser(User user);

	boolean moveUserByUid(Integer uid);

	boolean modifyUserByUid(User user);

	User findUserByUid(Integer uid);

	List findAllUser();

	List findUsersByUserame(String username);
}

b、class

package com.llangzh.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.llangzh.bean.User;
import com.llangzh.dao.IUserDao;
import com.llangzh.service.IUserService;

@Service
public class UserServiceImpl implements IUserService {

	@Autowired
	private IUserDao userDao;
	
	@Override
	public boolean addUser(User user) {
		return userDao.insertUser(user);
	}

	@Override
	public boolean moveUserByUid(Integer uid) {
		return userDao.deleteUserByUid(uid);
	}

	@Override
	public boolean modifyUserByUid(User user) {
		return userDao.updateUserByUid(user);
	}

	@Override
	public User findUserByUid(Integer uid) {
		return userDao.selectUserByUid(uid);
	}

	@Override
	public List findAllUser() {
		return userDao.selectAllUser();
	}

	@Override
	public List findUsersByUserame(String username) {
		return null;
	}
}

3、dao

a、interface

package com.llangzh.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.llangzh.bean.User;

@Repository
public interface IUserDao {

	boolean insertUser(User user);
	
	boolean deleteUserByUid(Integer uid);
	
	boolean updateUserByUid(User user);
	
	User selectUserByUid(Integer uid);
	List selectAllUser();
	List selectUsersByUserame(String username);
}

b、xml




	
		INSERT INTO `user`(username,password,age,email,address) VALUES (#{username},#{password},#{age},#{email},#{address})
	
	
	
		DELETE FROM `user` WHERE uid = #{uid}
	

	
		UPDATE `user` SET
			
				username = #{username},
			
			
				password = #{password},
			
				age = #{age},
				email = #{email},
				address = #{address}
			
				uid = #{uid}
			
	

	
		
		
		
		
		
		
	
	

	

六、启动类

package com.llangzh;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.llangzh.dao")
public class App {

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}

}

七、页面

1、add.html





addUser


	
用户名:
密码:
邮箱:
年龄:
地址:

2、modify.html





modifyUser



	

用户名:
密码:
年龄:
邮箱:
地址:

3、userinfo.html





userinfo



	
用户ID 用户姓名 用户密码 用户邮箱 用户年龄 用户地址 操作
修改用户 删除用户

4、fail.html





fail page


	fail page

失败操作全部转到这里。。。。

你可能感兴趣的:(学习记录,spring,boot)