spring boot整合mybatis的xml方式
1、在pom.xml里面除了引入基本的父工程和starter所生成的依赖外,还需引入如下坐标
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- druid连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.15</version> </dependency> <!-- SpringBoot的Mybatis启动器 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency>
2、application.properties文件配置
spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/dbgirl spring.datasource.username=root spring.datasource.password=root spring.datasource.type=com.alibaba.druid.pool.DruidDataSource mybatis.config-locations=classpath:mybatis/mybatis-config.xml mybatis.mapper-locations=classpath:mybatis/mapper/*.xml mybatis.type-aliases-package=com.testxml.domain
注意:如果mybatis的xml文件和mapper接口如果对应不起来的话,会报找不到方法的错误
3、mybatis-config.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> <settings> <setting name="callSettersOnNulls" value="true"/> <setting name="cacheEnabled" value="true"/> <setting name="lazyLoadingEnabled" value="true"/> <setting name="aggressiveLazyLoading" value="true"/> <setting name="multipleResultSetsEnabled" value="true"/> <setting name="useColumnLabel" value="true"/> <setting name="useGeneratedKeys" value="false"/> <setting name="autoMappingBehavior" value="PARTIAL"/> <setting name="defaultExecutorType" value="SIMPLE"/> <setting name="mapUnderscoreToCamelCase" value="true"/> <setting name="localCacheScope" value="SESSION"/> <setting name="jdbcTypeForNull" value="NULL"/> </settings> <typeAliases> <typeAlias alias="Integer" type="java.lang.Integer" /> <typeAlias alias="Long" type="java.lang.Long" /> <typeAlias alias="HashMap" type="java.util.HashMap" /> <typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" /> <typeAlias alias="ArrayList" type="java.util.ArrayList" /> <typeAlias alias="LinkedList" type="java.util.LinkedList" /> </typeAliases> </configuration> 路径在application.properties配置
4 、实体类:
package com.testxml.domain; public class Girl { private Integer id; private Integer age; private String cup_size; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getCup_size() { return cup_size; } public void setCup_size(String cup_size) { this.cup_size = cup_size; } @Override public String toString() { return "Girl{" + "id=" + id + ", age=" + age + ", cup_size='" + cup_size + '\'' + '}'; } }
5、mapper接口:
package com.testxml.dao; import com.testxml.domain.Girl; import java.util.List; public interface GirlMapper { List<Girl> findAll(); }
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.testxml.dao.GirlMapper"> <select id="findAll" resultType="com.testxml.domain.Girl"> select * from girl </select> </mapper>
6、service和实现类:
public interface GirlService { List<Girl> findAll(); }
@Service public class GirlServiceImpl implements GirlService { @Autowired private GirlMapper girlMapper; @Override public List<Girl> findAll() { return girlMapper.findAll(); } }
7、controller:
@RestController public class GirlController { @Autowired private GirlService girlService; @RequestMapping(value = "/girl",method = RequestMethod.GET) public void girl(){ List<Girl> girlList = girlService.findAll(); for (Girl girl:girlList){ System.out.println(girl.toString()); } }
8、启动类配置扫描注解:
@SpringBootApplication @MapperScan("com.testxml.dao") public class TestxmlApplication { public static void main(String[] args) { SpringApplication.run(TestxmlApplication.class, args); } }
9、启动服务,访问路径:http://localhost:8080/girl
项目目录结构: