国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > WEB開發 > 正文

spring boot 整合mybatis

2024-04-27 15:03:40
字體:
來源:轉載
供稿:網友

目錄

目錄sPRing boot 整合mybatispomxml依賴applicationproperties配置使用MyBatis

本篇所有代碼均來翟永超的博客: - 本文僅供自己學習之用

spring boot 整合mybatis

工作中所使用的框架spring boot + mybatis工作之余看到了搭建方法,順便把它放到這里,以供以后參考

pom.xml依賴

在pom.xml引入所需要的依賴 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <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> <version>5.1.21</version> </dependency> </dependencies>

application.properties配置

在application.properties中配置mysql的連接配置spring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.username=rootspring.datasource.passWord=rootspring.datasource.driver-class-name=com.mysql.jdbc.Driver

使用MyBatis

在Mysql中創建User表,包含id(BIGINT)、name(INT)、age(VARCHAR)字段。同時,創建映射對象Userpublic class User { private Long id; private String name; private Integer age; // 省略getter和setter}創建User映射的操作UserMapper,為了后續單元測試驗證,實現插入和查詢操作@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);}創建Spring Boot主類@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}創建單元測試 測試邏輯:插入一條name=AAA,age=20的記錄,然后根據name=AAA查詢,并判斷age是否為20測試結束回滾數據,保證測試單元每次運行的數據環境獨立@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = Application.class)@Transactionalpublic class ApplicationTests { @Autowired private UserMapper userMapper; @Test @Rollback public void findByName() throws Exception { userMapper.insert("AAA", 20); User u = userMapper.findByName("AAA"); Assert.assertEquals(20, u.getAge().intValue()); }}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大悟县| 大关县| 哈巴河县| 枣庄市| 澎湖县| 延川县| 洛扎县| 丹阳市| 家居| 叶城县| 吉林市| 中宁县| 景德镇市| 天镇县| 通城县| 旅游| 福清市| 屯门区| 天台县| 清远市| 娱乐| 平顶山市| 天峻县| 章丘市| 淄博市| 桦川县| 平乐县| 华宁县| 丽江市| 汨罗市| 什邡市| 宁远县| 项城市| 云霄县| 五大连池市| 碌曲县| 雅安市| 绥棱县| 仙桃市| 区。| 铜陵市|