1.导入引用
<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.26</version>
</dependency>
数据库连接池
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
2.配置application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/journalism
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.driverClassName=com.mysql.jdbc.Driver
##数据库连接池配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
##别名
mybatis.type-aliases-package=com.xinwen.pojo
#加载mapper.xml文件
mybatis.mapper-locations=classpath:mybatis/mappers/*.xml
配置UserMapper.xml文件
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.com.xinwen.mapper.UsersMapper">
<resultMap type="cn.com.xinwen.pojo.Users" id="usermap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="sex" property="sex"/>
<result column="age" property="age"/>
</resultMap>
<select id="getUser" parameterType="int" resultMap="usermap">
select * from users where id=#{id}
</select>
</mapper>
UserMapper.java接口
public interface UsersMapper {
public Users getUser(int id);
}
springboot 启动类
@SpringBootApplication
@ServletComponentScan
@MapperScan({"cn.com.xinwen.mapper"}) //必须添加扫描mapper接口
@ComponentScan("cn.com.xinwen")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}