SpringBoot Mybatis 入门
Mybatis for Java API官方文档:http://www.mybatis.org/mybatis-3/zh/java-api.html
Mybatis语法介绍
@Select 查询,所有的查询均使用这个
@Insert 插入,直接传入实体类会自动解析属性到对应的值
@Update 修改,也可以直接传入对象
@Delete 删除
@Result 修饰返回的结果集,关联实体类属性和数据库字段一一对应,如果实体类属性和数据库属性名保持一致,就不需要这个属性来修饰。
这里需要注意,使用#符号和$符号的不同:
// This example creates a prepared statement, something like select * from teacher where name = ?; @Select("Select * from teacher where name = #{name}") Teacher selectTeachForGivenName(@Param("name") String name); // This example creates n inlined statement, something like select * from teacher where name = 'someName'; @Select("Select * from teacher where name = '${name}'") Teacher selectTeachForGivenName(@Param("name") String name);
使用步骤
1、在Maven配置文件中,添加mybatis和mysql依赖
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
2、application.properties添加数据库配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://172.16.65.200/hibernate?characterEncoding=UTF8 spring.datasource.username=root spring.datasource.password=123456 mybatis.type-aliases-package=com.vmware.SpringMVC.domain // Mybatis Mapper接口类的路径
Springboot会自动加载spring.datasource.*相关配置,数据源就会自动注入到sqlSessionFactory中,sqlSessionFactory会自动注入到Mapper中,对了你一切都不用管了。
3、编写Mapper接口类
public interface UserMapper { @Select("SELECT * FROM users") @Results({ @Result(property = "userSex", column = "user_sex", javaType = UserSexEnum.class), @Result(property = "nickName", column = "nick_name") }) List<UserEntity> getAll(); @Select("SELECT * FROM users WHERE id = #{id}") @Results({ @Result(property = "userSex", column = "user_sex", javaType = UserSexEnum.class), @Result(property = "nickName", column = "nick_name") }) UserEntity getOne(Long id); @Insert("INSERT INTO users(userName,passWord,user_sex) VALUES(#{userName}, #{passWord}, #{userSex})") void insert(UserEntity user); @Update("UPDATE users SET userName=#{userName},nick_name=#{nickName} WHERE id =#{id}") void update(UserEntity user); @Delete("DELETE FROM users WHERE id =#{id}") void delete(Long id);
4、在Service业务逻辑层去调用Mapper接口
// 业务逻辑层调用Mapper
@Service public class UserService { @Autowired private UserMapper userMapper; public List<User> getAll(){ return userMapper.getAll(); } }
// Controller调用Service
@RestController @RequestMapping("user") public class UserController { @Autowired private UserService userService; @GetMapping("/all") public List<User> getAll(){ return userService.getAll(); } }
5、在启动类中添加对Mapper包扫描@MapperScan()
/** * @MapperScan() 表示扫描MyBatis的Mapper接口所在的包,找出所有加了@Mapper或@Repository注解的接口。 */ @SpringBootApplication @MapperScan("com.vmware.SpringMVC.domain") public class SpringMvcApplication { public static void main(String[] args) { SpringApplication.run(SpringMvcApplication.class, args); } }
参考资料:
http://412887952-qq-com.iteye.com/blog/2391924
https://blog.csdn.net/didi7696/article/details/80117238
https://cloud.tencent.com/developer/article/1347912