1.在數(shù)據(jù)庫中新建一個數(shù)據(jù)庫,添加一張表
2.創(chuàng)建一個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中實現(xiàn)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接口編寫數(shù)據(jù)庫的增刪改查方法
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中添加數(shù)據(jù)庫驅(qū)動和指定文件映射
##數(shù)據(jù)庫配置
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);
}
}