org.springframework.boot
spring-boot-starter-parent
2.3.2.RELEASE
8
8
UTF-8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.1
mysql
mysql-connector-java
5.1.47
com.alibaba
druid
1.2.19
org.springframework.boot
spring-boot-starter-thymeleaf
src/main/java
**/*.xml
src/main/resources
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3305/springboot
username: root
password:
type: com.alibaba.druid.pool.DruidDataSource
logging:
level:
com:
by:
mapper: DEBUG
@SpringBootApplication
@MapperScan("com.by.mapper")
public class SpringbootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisApplication.class, args);
}
}
添加用户
新增用户
/*
* Copyright (c) 2020, 2024, All rights reserved.
*
*/
package com.by.controller;
import com.by.pojo.User;
import com.by.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* Project: springboot - UserController
* Powered by scl On 2024-01-15 16:25:51
* 描述:
*
* @author 孙臣龙 [[email protected]]
* @version 1.0
* @since 17
*/
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
/**
* 添加
*
* @param user
* @return
*/
@RequestMapping("/addUser")
public String addUser(User user) {
userService.addUser(user);
return "redirect:/user/selectUser";
}
}
/*
* Copyright (c) 2020, 2024, All rights reserved.
*
*/
package com.by.service;
import com.by.pojo.User;
import java.util.List;
public interface UserService{
void addUser(User user);
}
实现类:
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public void addUser(User user) {
userMapper.addUser(user);
}
}
package com.by.mapper;
import com.by.pojo.User;
import java.util.List;
/**
* Project: springboot - UserMapper
* Powered by scl On 2024-01-15 14:51:39
* 描述:
*
* @author 孙臣龙 [[email protected]]
* @version 1.0
* @since 17
*/
public interface UserMapper {
void addUser(User user);
}
映射文件:
insert into user(name,sex,pwd,birth) values(#{name},#{sex},#{pwd},#{birth})
首页
/**
* 查询所有用户信息
*
* @param model
* @return
*/
@RequestMapping("/selectUser")
public String selectUser(Model model) {
List userList = userService.selectUser();
model.addAttribute("userList", userList);
return "select_user";
}
List selectUser();
@Override
public List selectUser() {
return userMapper.selectUser();
}
List selectUser();
/**
* 根据id删除用户
*
* @param id
* @return
*/
@GetMapping("/deleterUserById/{id}")
public String deleterUserById(@PathVariable Integer id) {
userService.deleterUserById(id);
return "redirect:/user/selectUser";
}
void deleterUserById(Integer id);
@Override
public void deleterUserById(Integer id) {
userMapper.deleterUserById(id);
}
void deleterUserById(Integer id);
delete from user where id =#{id}
添加用户
新增用户
/**
* 数据回显
*
* @param id
* @return
*/
@GetMapping("/getUserById/{id}")
public String getUserById(@PathVariable Integer id, Model model) {
User user = userService.getUserById(id);
model.addAttribute("user", user);
return "update_user";
}
/**
* 修改
*
* @param
* @return
*/
@PostMapping("/updateUserById")
public String updateUserById(User user) {
userService.updateUserById(user);
return "redirect:/user/selectUser";
}
User getUserById(Integer id);
void updateUserById(User user);
@Override
public User getUserById(Integer id) {
return userMapper.getUserById(id);
}
@Override
public void updateUserById(User user) {
userMapper.updateUserById(user);
}
User getUserById(Integer id);
void updateUserById(User user
update user
name=#{name},
sex=#{sex},
pwd=#{pwd},
birth=#{birth},
where id =#{id}
DateConverter:(日期转换器)
/*
* Copyright (c) 2020, 2024, All rights reserved.
*
*/
package com.by.converter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Project: springboot - DateConverter
* Powered by scl On 2024-01-15 17:09:15
* 描述:
*
* @author 孙臣龙 [[email protected]]
* @version 1.0
* @since 17
*/
/**
* 日期转换器
*/
@Component
public class DateConverter implements Converter {
@Override
public Date convert(String source) {
try {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.parse(source);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
WebmvcConfig:(将日期转换器装到容器中)
/*
* Copyright (c) 2020, 2024, All rights reserved.
*
*/
package com.by.config;
import com.by.converter.DateConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.FormatterRegistry;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* Project: springboot - WebmvcConfig
* Powered by scl On 2024-01-15 17:17:46
* 描述:
*
* @author 孙臣龙 [[email protected]]
* @version 1.0
* @since 17
*/
/**
* 将日期转换器注入到容器中
*/
@Component
public class WebmvcConfig implements WebMvcConfigurer {
@Autowired
private DateConverter dateConverter;
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(dateConverter);
}
}