spring boot mybatis
来源:互联网 发布:python财经数据接口包 编辑:程序博客网 时间:2024/06/13 22:18
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
application.properties中配置mysql的连接配置
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
@Mapperpublic interface UserMapper { @Select("SELECT * FROM USER WHERE NAME = #{name}") User findByName(@Param("name") String name); @Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})") int insert(@Param("name") String name, @Param("age") Integer age);}
传参方式
下面通过几种不同传参方式来实现前文中实现的插入操作。
使用@Param
在之前的整合示例中我们已经使用了这种最简单的传参方式,如下:
@Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})")int insert(@Param("name") String name, @Param("age") Integer age);
这种方式很好理解,@Param中定义的name对应了SQL中的#{name},age对应了SQL中的#{age}。
使用Map
如下代码,通过Map对象来作为传递参数的容器:
@Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER})")int insertByMap(Map<String, Object> map);
对于Insert语句中需要的参数,我们只需要在map中填入同名的内容即可,具体如下面代码所示:
Map<String, Object> map = new HashMap<>();map.put("name", "CCC");map.put("age", 40);userMapper.insertByMap(map);
使用对象
除了Map对象,我们也可直接使用普通的Java对象来作为查询条件的传参,比如我们可以直接使用User对象:
@Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})")int insertByUser(User user);
这样语句中的#{name}、#{age}就分别对应了User对象中的name和age属性。
增删改查
MyBatis针对不同的数据库操作分别提供了不同的注解来进行配置,在之前的示例中演示了@Insert,下面针对User表做一组最基本的增删改查作为示例:
public interface UserMapper { @Select("SELECT * FROM user WHERE name = #{name}") User findByName(@Param("name") String name); @Insert("INSERT INTO user(name, age) VALUES(#{name}, #{age})") int insert(@Param("name") String name, @Param("age") Integer age); @Update("UPDATE user SET age=#{age} WHERE name=#{name}") void update(User user); @Delete("DELETE FROM user WHERE id =#{id}") void delete(Long id);}
返回结果的绑定
对于增、删、改操作相对变化较小。而对于“查”操作,我们往往需要进行多表关联,汇总计算等操作,那么对于查询的结果往往就不再是简单的实体对象了,往往需要返回一个与数据库实体不同的包装类,那么对于这类情况,就可以通过@Results和@Result注解来进行绑定,具体如下:
@Results({ @Result(property = "name", column = "name"), @Result(property = "age", column = "age")})@Select("SELECT name, age FROM user")List<User> findAll();
在上面代码中,@Result中的property属性对应User对象中的成员名,column对应SELECT出的字段名。在该配置中故意没有查出id属性,只对User对应中的name和age对象做了映射配置,这样可以通过下面的单元测试来验证查出的id为null,而其他属性不为null:
@Test@Rollbackpublic void testUserMapper() throws Exception { List<User> userList = userMapper.findAll(); for(User user : userList) { Assert.assertEquals(null, user.getId()); Assert.assertNotEquals(null, user.getName()); }}
http://www.mybatis.org/mybatis-3/zh/java-api.html
- Spring boot Mybatis
- Spring Boot 集成MyBatis
- spring boot mybatis
- Spring Boot + mybatis 开发
- Spring Boot 集成Mybatis
- spring-boot集成mybatis
- Spring Boot整合MyBatis
- spring boot 整合mybatis
- Spring Boot整合MyBatis
- Spring-boot整合Mybatis
- Spring boot集成Mybatis
- Spring Boot整合MyBatis
- spring boot mybatis
- spring boot+Mybatis
- spring boot 整合mybatis
- Spring Boot 集成MyBatis
- spring boot集成mybatis
- spring-boot-mybatis
- JuniperSRX---------rpm配合track完成双线主备
- bzoj 1483: [HNOI2009]梦幻布丁 线段树合并
- oracle做了update的误操作,怎么恢复之前数据
- 剑指offer刷题记录2
- 计算经纬度距离
- spring boot mybatis
- PDO中包含三个预定义的类--PDO、PDOStatement和PDOException
- Oracle复杂查询案例
- 进制之间相互转换工具
- spring-boot项目在外部tomcat环境下部署
- 文章标题 linux 系统性能查看即监控详解
- OData demo 2
- 搜索二叉树的错误节点
- lvs+keepalived+nginx几种模式的比较