SpringBoot整合Mybatis-方式2:mapper使用XML配置方式
上一篇博文已经详细介绍了SpringBoot环境搭建过程,这一节介绍一下SpringBoot整合Mybatis,在mapper中数据库的查询操作使用的是注解的方式,本文将介绍使用XML的方式进行整合。
SpringBoot整合Mybatis
方式2:mapper使用XML
项目结构
1:准备数据库数据和配置数据源以及配置Mybatis的XML配置文件
#加载Mybatis配置文件 mybatis.mapper-locations = classpath:mapper/*Mapper.xml mybatis.config-location = classpath:config/sqlMapConfig.xml
#数据源必填项 spring.datasource.driver-class-name= com.mysql.jdbc.Driver spring.datasource.url = jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=utf-8 spring.datasource.username = root spring.datasource.password = 123456
#选填 # Number of ms to wait before throwing an exception if no connection is available. spring.datasource.tomcat.max-wait=10000 # Maximum number of active connections that can be allocated from this pool at the same time. spring.datasource.tomcat.max-active=50 # Validate the connection before borrowing it from the pool. spring.datasource.tomcat.test-on-borrow=true |
2:编写Mapper.Xml和sqlMapConfig.xml
userMapper.xml |
<?xml version=“1.0” encoding=“UTF-8”?> <!DOCTYPE mapper PUBLIC “-//mybatis.org//DTD Mapper 3.0//EN” “http://mybatis.org/dtd/mybatis-3-mapper.dtd“> <mapper namespace=“com.liujia.springdemo.mapper.UserMapper” > <select id=“getAll” resultType=“com.liujia.springdemo.entity.User” > SELECT * FROM user </select> </mapper> |
sqlMapConfig.xml |
<?xml version=“1.0” encoding=“UTF-8” ?> <!DOCTYPE configuration PUBLIC “-//mybatis.org//DTD Config 3.0//EN” “http://mybatis.org/dtd/mybatis-3-config.dtd“> <configuration> <typeAliases> </typeAliases> </configuration> |
3:加maven依赖
<!— Spring-Mybatis —> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!— MySQL驱动 —> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> |
4:编写实体类
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private int id;// id
private String name;// 姓名
private int age;// 年龄
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
@Override public String toString() { return “User [id=” + id + “, name=” + name + “, age=” + age + “]”; }
} |
5:编写mapper
import com.liujia.springdemo.entity.User;
public interface UserMapper {
/* 获取所有的user对象
@return */ List<User> getAll(); } |
6:编写service层
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
import com.liujia.springdemo.entity.User; import com.liujia.springdemo.mapper.UserMapper;
@Service public class UserService { @Autowired private UserMapper userMapper;
public List<User> getAll() { return userMapper.getAll(); }; } |
7:编写Copntroller层
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import com.liujia.springdemo.entity.User; import com.liujia.springdemo.service.UserService;
@RestController public class UserController {
@Autowired private UserService userService;
@RequestMapping(“/getallusers”) public List<User> getAllUsers() { return userService.getAll(); } } |
8:增加包扫描和运行程序
@SpringBootApplication @MapperScan(basePackages = { “com.liujia.springdemo.mapper”}) public class Application {
public static void main(String[] args) { SpringApplication.run(Application.class, args); }
@Bean public CommandLineRunner commandLineRunner(ApplicationContext ctx) { return args -> { System.out.println(“Let’s inspect the beans provided by Spring Boot:”); String[] beanNames = ctx.getBeanDefinitionNames(); Arrays.sort(beanNames); for (String beanName : beanNames) { System.out.println(beanName); } }; } } |
运行结果
参考文献
https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#boot-features-using-jdbc-template
https://blog.csdn.net/saytime/article/details/74783296
总结:
两种方式的区别
1:是否需要在application.properties中配置mapper.xml和sqlMapperConfig.xml
2:将SQL语句写在了XML中
由于第二种方式将比较繁琐的SQL语句写在了配置文件中,整个代码结构更加清晰,本人更加喜欢此种配置方式。
源码下载地址
https://download.csdn.net/download/caoshangfeidie000/10670735
还没有评论,来说两句吧...