SpringBoot學習筆記(1):配置Mybatis
阿新 • • 發佈:2018-04-05
target oca run class .com gpo connect auto users
SpringBoot學習筆記(1):配置Mybatis
參考資料:
1.AndyLizh的博客
2.xiaolyuh123的博客
快速開始
添加Mybatis依賴(其他依賴已省去)
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency>
配置application.properties文件
server.port=80 # 數據源配置 spring.datasource.url=jdbc:mysql://localhost:3306/ssb_test spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=root #連接池配置 #spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource #mybatis #entity掃描的包名 mybatis.type-aliases-package=com.xiaolyuh.domain.model #Mapper.xml所在的位置 mybatis.mapper-locations=classpath*:/mybaits/*Mapper.xml #pagehelper分頁插件配置 pagehelper.helperDialect=mysql pagehelper.reasonable=true pagehelper.supportMethodsArguments=true pagehelper.params=count=countSql #日誌配置 logging.level.com.xiaolyuh=debug logging.level.org.springframework.web=debug logging.level.org.springframework.transaction=debug logging.level.org.mybatis=debug debug=false
mybatis除了以上兩種常見配置外,還有:
mybatis.config-location=mybatis-config.xml配置文件的路徑
mybatis.type-handlers-package=掃描typeHandlers的包
mybatis.check-config-location=檢查配置文件是否存在
mybatis.executor-type=設置執行模式(SIMPLE, REUSE, BATCH),默認為SIMPLE
開發Mapper
@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); }
開始使用
上面三步就基本完成了相關dao層開發,使用的時候當作普通的類註入即可。
@RunWith(SpringRunner.class) @SpringBootTest public class UserMapperTest { @Autowired private UserMapper UserMapper; @Test public void testInsert() throws Exception { UserMapper.insert(new UserEntity("aa", "a123456", UserSexEnum.MAN)); UserMapper.insert(new UserEntity("bb", "b123456", UserSexEnum.WOMAN)); UserMapper.insert(new UserEntity("cc", "b123456", UserSexEnum.WOMAN)); Assert.assertEquals(3, UserMapper.getAll().size()); } @Test public void testQuery() throws Exception { List<UserEntity> users = UserMapper.getAll(); System.out.println(users.toString()); } @Test public void testUpdate() throws Exception { UserEntity user = UserMapper.getOne(3l); System.out.println(user.toString()); user.setNickName("neo"); UserMapper.update(user); Assert.assertTrue(("neo".equals(UserMapper.getOne(3l).getNickName()))); } }
Mapper的註冊方式
啟動類添加註解
@SpringBootApplication @MapperScan("com.kerry.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
每個接口類單獨添加註解
package com.kerry.mapper; import java.util.List; import org.apache.ibatis.annotations.Mapper; import com.kerry.model.User; @Mapper public interface UserMapper { List<User> getAll(); User getOne(Integer id); void insert(User user); void update(User user); void delete(Integer id); }
SpringBoot學習筆記(1):配置Mybatis