如果你希望试一试那么直接复制运行,如果你先更进一步了解就仔细看看代码,花上5-30分钟然后可以重新应该能理解。然后就自己结合自己需求改改吧。
POM.xml
<!-- SpringBoot 核心包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- SpringBoot 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Mysql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- SpringBoot集成mybatis框架 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.boot.starter.version}</version>
</dependency>
<!--阿里数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
application.yml
# Spring配置
spring:
#数据源
datasource:
#url
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
#用户名
username: root
#密码
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
# 配置mapper的扫描,找到所有的mapper.xml映射文件
mapper-locations: classpath:mybatis/**/*.xml
# 搜索指定包别名
typeAliasesPackage: com.slow.nofast
@SpringBootApplication
@MapperScan("com.slow.nofast.*.*.mapper")
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
domain
public class TestUserDomain {
private Long userid;
private String name;
public Long getUserid() {
return userid;
}
public void setUserid(Long userid) {
this.userid = userid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
mapper
public interface UserMapper {
public TestUserDomain selectTest();
}
<?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.slow.nofast.system.user.mapper.UserMapper">
<resultMap type="TestUserDomain" id="UserResult">
<id property="userid" column="user_id" />
<result property="name" column="user_name" />
</resultMap>
<select id ="selectTest" resultMap="UserResult">
select user_id,user_name from sys_user limit 1
</select>
</mapper>
Test
@SpringBootTest(classes=Application.class)
@RunWith(SpringRunner.class)
public class DruidTest {
@Autowired
UserMapper userMapper;
@Test
public void druidTest() {
TestUserDomain selectTest = userMapper.selectTest();
System.out.println(selectTest.getName());
}
}