=======spring boot 2.0 + Mybatis整合篇之XML,注解=======================================
1.pom.xml添加:--------------------------------------------------------------------------
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.9</version> </dependency> 2.第2步分两个分支同步进行,支持XML和注解两种方式,如果SQL中没有Left join等高级关联查询,用注解方式即可解决, 如果有高级查询,需要配合XML方式写高级SQL语句。---------------------------------------------------------------2.1 XML方式:接口类UserMapper
2.1.1 application.properties文件中添加:----------------------------------------------------- mybatis.config-location=classpath:mybatis-config.xmlserver.port=81
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.url=jdbc:mysql://localhost:3306/mybaitsdemo spring.datasource.username=root spring.datasource.password= spring.datasource.driver-class-name=com.mysql.jdbc.Driver2.1.2 建立实体类:-------------------------------------------------------------------------------
//需要建立set get方法,此处暂且省略了。 public class User {private Long id;
private Integer age; private String name;public User() {
}
}--接口类-Dao包中-------------------------------------------------------------------
@Component
public interface UserM {User findById(@Param("id") Long id);
}
--------------------------------------------------------------
2.1.3 resources文件夹目录添加: 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> <mappers> <mapper resource="mapper/UserMapper.xml"/> </mappers> </configuration>2.1.4 resources文件夹目录添加: mapper目录,在mapper目录里添加UserMapper.xml-------------------
------------------添加完后的目录结构:resources/mapper/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.lemi.xxxx"> <select id="selectById" resultType="com.lemi.mydemo.entity.User"> select * from user where id = #{id} </select> </mapper>
2.1.5 在UserDao类中定义: 属性,sqlSession对象在spring boot加载的IOC容器中自动建立
并且把application.properties文件中配置的DataSource的值赋给sqlSession对象。------------------------------ @Component //UserDao必须加 @Component 注解,在controller中可以自动注入 @Autowired public UserDao class{@Autowired
private SqlSession sqlSession;public User selectById(long id) {
return this.sqlSession.selectOne("selectById", id); //sqlSession.selectOne(String,gvr)
}
} 2.1.6 在controller包下添加: @RestController public class Home {@Autowired
private UserDao userDao;@RequestMapping("/")
public User index(){return userDao.selectById(1);
} }2.1.7 运行程序测试是否正常,浏览器中输入:http:localhost:81/ ----------------------------------------------------------
2.2 注解方式:注解方式下必须要Mapper接口类-------------------------------------------------------
2.2.1 application.properties文件中添加:-----------------------------------------------------
##mybatis.config-location=classpath:mybatis-config.xml (注解方式下不需要)server.port=81
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.url=jdbc:mysql://localhost:3306/mybaitsdemo spring.datasource.username=root spring.datasource.password= spring.datasource.driver-class-name=com.mysql.jdbc.Driver2.2.2 建立实体类:-------------------------------------------------------------------------------
//需要建立set get方法,此处暂且省略了。 public class User {private Long id;
private Integer age; private String name;public User() {
}
}2.2.3 根据实体类建立Mapper接口类文件:UserMapper或UserM 名字随便起意义最好与实体类有关联 -------
@Component
public interface UserM {//注解形式配置SQL
@Select("select * from user where id = #{id} ") User findById(@Param("id") Long id);}
2.2.4在UserDao类中定义:------------------------------------------------------------------------- @Component public UserDao class{ @Autowired private UserMapper userMapper;public User selectById(long id) {
return userMapper.findById(id); } } 2.2.5 在程序启动类中添加 @MapperScan("com.lemi.mydemo.dao")------------------------------------ ------------------指定UserMapper接口类的包名,也可以是com.lemi.mydemo.* -----------------@SpringBootApplication
@MapperScan("com.lemi.mydemo.dao") public class MydemoApplication {public static void main(String[] args) {
SpringApplication.run(MydemoApplication.class, args); } }2.2.6 在controller包下添加 @RestController public class Home {@Autowired
private UserDao userDao;@RequestMapping("/")
public User index(){return userDao.selectById(1);
} }2.2.7 运行程序测试是否正常,浏览器中输入:http:localhost:81/ ----------------------------------------------------------