spring boot(三) 集成mybatis
2018-06-18 02:23:38来源:未知 阅读 ()
前言
随着spring boot2.0的发布。项目组的API接口已经考虑向spring boot转型。底层接口我们一直用的mybatis,所以这篇文章我特意练习了下在spring boot种集成mybatis。
一、准备工作
1、pom.xml
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot</artifactId> 5 <version>2.0.0.RELEASE</version> 6 </dependency> 7 <dependency> 8 <groupId>org.springframework.boot</groupId> 9 <artifactId>spring-boot-starter-web</artifactId> 10 <version>2.0.0.RELEASE</version> 11 </dependency> 12 <dependency> 13 <groupId>com.microsoft.sqlserver</groupId> 14 <artifactId>sqljdbc4</artifactId> 15 <version>4.0</version> 16 </dependency> 17 <dependency> 18 <groupId>org.mybatis.spring.boot</groupId> 19 <artifactId>mybatis-spring-boot-starter</artifactId> 20 <version>1.3.2</version> 21 </dependency> 22 <dependency> 23 <groupId>com.alibaba</groupId> 24 <artifactId>druid</artifactId> 25 <version>1.1.9</version> 26 </dependency> 27 <dependency> 28 <groupId>org.springframework.boot</groupId> 29 <artifactId>spring-boot-starter-test</artifactId> 30 <version>2.0.0.RELEASE</version> 31 <scope>test</scope> 32 </dependency> 33 </dependencies>
2、项目结构
配置文件依然放在resources目录下,spring boot中支持properties、也支持yml的方式。

3、使用注解的方式编写UserMapper
public interface UserDao {
@Select("select * from tb_user")
List<User> getAllUsers();
@Select("select * from tb_user where id=#{id}")
User getById(int id);
@Insert("insert into tb_user(name,address) values(#{name},#{address})")
void insert(User user);
@Update("update tb_user set name=#{name},address=#{address} where id=#{id}")
void update(User user);
@Delete("delete from tb_user where id=#{id}")
void delete(int id);
}
启动类 Application.java。 主要是MapperScan注解,配置映射包目录com.che168.dao
@SpringBootApplication
@MapperScan("com.che168.dao")
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class,args);
}
}
单元测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserDaoTest {
@Autowired
UserDao userDao;
@Test
public void getAllUsers(){
List<User> allUsers=userDao.getAllUsers();
System.out.println(allUsers.size());
}
@Test
public void getById(){
int id=1;
User model=userDao.getById(id);
System.out.println("name:"+model.getName()+",address:"+model.getAddress());
}
@Test
public void insert(){
User user=new User();
user.setName("云龙");
user.setAddress("山西太原");
userDao.insert(user);
}
@Test
public void update(){
User user=new User();
user.setId(4);
user.setName("云龙");
user.setAddress("山西运城");
userDao.update(user);
}
@Test
public void delete(){
userDao.delete(4);
}
}
二、使用配置文件的方式
配置文件的方式和我们之前在SpringMVC中集成的方式大致相同,把xml文件单独放在mapper目录下,把sql相关操作全部放在xml中, 接口用来编写方法签名,然后配置映射,在spring boot中不同的地方就是把mapper-config.xml和实体类映射文件需要配置在application.properties中,如下配置,在此就不再赘述了。
mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Spring系列.ApplicationContext接口 2020-06-11
- springboot2配置JavaMelody与springMVC配置JavaMelody 2020-06-11
- 给你一份超详细 Spring Boot 知识清单 2020-06-11
- SpringBoot 2.3 整合最新版 ShardingJdbc + Druid + MyBatis 2020-06-11
- 掌握SpringBoot-2.3的容器探针:实战篇 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
