1.在数据库中新建一个数据库,添加一张表
2.创建一个Spring Initializr模块
3.在pom.z中添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<optional>true</optional>
</dependency>
4.在自动生成的包下新建entity包、service包、serviceimpl包、mapper包、controller包
5.在entity中新建User类
@Data
public class Course {
private Long courseId;
private String courseName;
private Long userId;
private String courseClass;
private String cover;
private String courseCode;
private Short finished;
}
(注解不能少,且所有的属性都用封装属性);
6.在service中新建UserService接口,serviceimpl中实现service方法
public interface UserService {
List<User> selectAll();
User getOne(long userId);
void delete(long userId);
void insert(User user);
void update(User user );
}
@Service
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public List<User> selectAll() {
return userMapper.selectAll();
}
@Override
public User getOne(long userId) {
return userMapper.getOne(userId);
}
@Override
public void delete(long userId) {
userMapper.delete(userId);
}
@Override
public void insert(User user) {
userMapper.insert(user);
}
@Override
public void update(User user) {
userMapper.update(user);
}
}
(注解不要漏掉)
7.在mapper中新建UserMapper接口编写数据库的增删改查方法
public interface UserMapper {
@Results({@Result(column = "user_id",property = "userId"),
@Result(column = "mobile",property = "mobile"),
@Result(column = "password",property = "password"),
@Result(column = "username",property = "username"),
@Result(column = "avatar",property = "avatar")
})
@Select("SELECT * FROM t_sys_user ")
List<User> selectAll();
@Results({@Result(column = "user_id",property = "userId"),
@Result(column = "mobile",property = "mobile"),
@Result(column = "password",property = "password"),
@Result(column = "username",property = "username"),
@Result(column = "avatar",property = "avatar")
})
@Select("SELECT * FROM t_sys_user WHERE user_id = #{userId} ")
User getOne(Long userId);
@Delete("DELETE FROM t_sys_user WHERE user_id = #{userId} ")
void delete(Long userId);
@Insert("INSERT INTO t_sys_user(mobile,password,username,avatar) VALUES(#{mobile},#{password},#{username},#{avatar})")
void insert(User user);
@Update("UPDATE t_sys_user SET username=#{username},password=#{password},avatar=#{avatar} WHERE user_id=#{userId} ")
void update(User user);
}
8.在resources的application.properties中添加数据库驱动和指定文件映射
##数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/db_spring?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#指定实体类映射的包
mybatis.type-aliases-package=com.springboot.mybatis.entity
9.对UserServiceImpl进行单元测试,无异常通过CRUD
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceImplTest {
@Resource
private UserService userService;
@Test
public void selectAll() {
List<User> userList=userService.selectAll();
userList.forEach(user -> System.out.println(user));
}
@Test
public void getOne() {
User user=userService.getOne(1);
System.out.println(user);
}
@Test
public void delete() {
userService.delete(1);
}
@Test
public void insert() {
User user=new User();
user.setMobile("14531321864");
user.setPassword("444");
user.setUsername("张壮壮");
user.setAvatar("user4.jpg");
userService.insert(user);
}
@Test
public void update() {
User user=userService.getOne(3L);
user.setAvatar("1.jpg");
userService.update(user);
}
}
(注解!)
9.运行SpringbootMybatisApplication
@SpringBootApplication
@MapperScan("com.springboot.mybatis.mapper")
public class SpringbootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisApplication.class, args);
}
}
(SpringbootMybatisApplication为自动生成,只需在类上在添加MapperScan注解)
10.在controller中新建UserController类
@RestController
@RequestMapping(value = "/api")
public class UserController {
@Resource
private UserService userService;
@RequestMapping(value = "/users",method = RequestMethod.GET)
public List<User> selectAll(){
return userService.selectAll();
}
@RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
public User getOne(@PathVariable("id") long id){
return userService.getOne(id);
}
@RequestMapping(value = "/user/{id}",method = RequestMethod.DELETE)
public void delete(@PathVariable("id") long id){
userService.delete(id);
}
@RequestMapping(value = "/user",method = RequestMethod.POST)
public void addUser(@RequestBody User user){
userService.insert(user);
}
@RequestMapping(value = "/user",method = RequestMethod.PUT)
public void updateUser(@RequestBody User user){
userService.update(user);
}
}