spring boot 2.0.1整合MyBatis
2018-03-21 12:14
337 查看
1.导入依赖
<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>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>2.定义数据源
spring.thymeleaf.cache=false
spring.datasource.url=jdbc:mysql://localhost:3306/blogdata?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.type-aliases-package=com.example.bean //模型类包
3.目录结构
4.User.class
package com.example.bean;
public class User {
private int id;
private String email;
private String nickname;
private String password;
public User() {
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getNickname() {
return nickname;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
// Getter、Setter
}UserMapper.class
package com.example.mapper;
import com.example.bean.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
// @Mapper 这里可以使用@Mapper注解,但是每个mapper都加注解比较麻烦,所以统一配置@MapperScan在扫描路径在application类中
public interface UserMapper {
@Select("SELECT * FROM user_info WHERE id = #{id}")
User getUserById(Integer id);
@Select("SELECT * FROM us
4000
er_info")
public List<User> getUserList();
@Insert("insert into tb_user(username, age, ctm) values(#{username}, #{age}, now())")
public int add(User user);
@Update("UPDATE tb_user SET username = #{user.username} , age = #{user.age} WHERE id = #{id}")
public int update(@Param("id") Integer id, @Param("user") User user);
@Delete("DELETE from tb_user where id = #{id} ")
public int delete(Integer id);
}UserService.class
package com.example.service;
import com.example.bean.User;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @ClassName cn.saytime.service.UserService
* @Description
*/
public interface UserService {
User getUserById(Integer id);
public List<User> getUserList();
public int add(User user);
public int update(Integer id, User user);
public int delete(Integer id);
}UserServiceimpl.class
package com.example.service.impl;
import com.example.bean.User;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @ClassName cn.saytime.service.impl.UserServiceImpl
* @Description
*/
@Service
public class UserServiceimpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User getUserById(Integer id) {
return userMapper.getUserById(id);
}
@Override
public List<User> getUserList() {
return userMapper.getUserList();
}
@Override
public int add(User user) {
return userMapper.add(user);
}
@Override
public int update(Integer id, User user) {
return userMapper.update(id, user);
}
@Override
public int delete(Integer id) {
return userMapper.delete(id);
}
}UserController.class
package com.example.web;
import com.example.bean.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/g")
public List<User> ha(){
return userService.getUserList();
}
}PS:入口类一定要放在众包之上。
<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>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>2.定义数据源
spring.thymeleaf.cache=false
spring.datasource.url=jdbc:mysql://localhost:3306/blogdata?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.type-aliases-package=com.example.bean //模型类包
3.目录结构
4.User.class
package com.example.bean;
public class User {
private int id;
private String email;
private String nickname;
private String password;
public User() {
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getNickname() {
return nickname;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
// Getter、Setter
}UserMapper.class
package com.example.mapper;
import com.example.bean.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
// @Mapper 这里可以使用@Mapper注解,但是每个mapper都加注解比较麻烦,所以统一配置@MapperScan在扫描路径在application类中
public interface UserMapper {
@Select("SELECT * FROM user_info WHERE id = #{id}")
User getUserById(Integer id);
@Select("SELECT * FROM us
4000
er_info")
public List<User> getUserList();
@Insert("insert into tb_user(username, age, ctm) values(#{username}, #{age}, now())")
public int add(User user);
@Update("UPDATE tb_user SET username = #{user.username} , age = #{user.age} WHERE id = #{id}")
public int update(@Param("id") Integer id, @Param("user") User user);
@Delete("DELETE from tb_user where id = #{id} ")
public int delete(Integer id);
}UserService.class
package com.example.service;
import com.example.bean.User;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @ClassName cn.saytime.service.UserService
* @Description
*/
public interface UserService {
User getUserById(Integer id);
public List<User> getUserList();
public int add(User user);
public int update(Integer id, User user);
public int delete(Integer id);
}UserServiceimpl.class
package com.example.service.impl;
import com.example.bean.User;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @ClassName cn.saytime.service.impl.UserServiceImpl
* @Description
*/
@Service
public class UserServiceimpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User getUserById(Integer id) {
return userMapper.getUserById(id);
}
@Override
public List<User> getUserList() {
return userMapper.getUserList();
}
@Override
public int add(User user) {
return userMapper.add(user);
}
@Override
public int update(Integer id, User user) {
return userMapper.update(id, user);
}
@Override
public int delete(Integer id) {
return userMapper.delete(id);
}
}UserController.class
package com.example.web;
import com.example.bean.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/g")
public List<User> ha(){
return userService.getUserList();
}
}PS:入口类一定要放在众包之上。
相关文章推荐
- 【SpringBoot系列1】SpringBoot整合MyBatis
- 二十、Spring Boot整合MyBatis(下)
- (十一)Spring Boot整合Mybatis使用druid实现多数据源自动切换
- Spring Boot 整合 MyBatis 并启用二级缓存
- spring boot + mybatis + jsp整合
- springboot整合mybatis使用阿里(阿里连接池)和xml方式
- springboot整合mybatis(xml版)
- Spring Boot2.0 整合mybatis、分页插件、druid
- SpringBoot整合Mybatis,简单明了
- Spring Boot 整合 MyBatis
- spring boot整合mybatis使用c3p0数据源连接mysql
- spring Boot整合mybatis
- Spring boot整合mybatis
- Spring Boot 构建应用——整合 Mybatis
- Spring Boot 整合 Mybatis 实现 Druid 多数据源详解
- spring boot 整合mybatis 通过官方mybatis-spring-boot-starter
- springBoot+Mybatis整合小案例《一》
- Springboot+mybatis 搭建并完美整合
- SpringBoot学习-(六)SpringBoot与Mybatis整合
- Spring boot Mybatis 整合(完整版)