1 整合Mybatis
1.1 导入依赖
- mybatis起步依赖
- mysql启动依赖
<dependencies>
<!-- mybatis起步依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!-- mysql启动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
1.2 SpringBoot配置
数据源配置
mybatis相关配置
加载mybatis配置文件
mybatis别名配置
日志配置
加载mapper映射文件配置文件
# 数据源配置
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///你的数据库名
username: root
password: 1234
mybatis:
# 加载mybatis配置文件
# config-location: mybatis-config.xml
#mybatis别名配置
type-aliases-package: com.ying.springboot01.pojo
configuration:
#日志配置
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 开启驼峰映射配置
map-underscore-to-camel-case: true
#加载mapper映射文件配置文件(当mapper映射文件与Mapper接口不在统一目录下需要配置)
mapper-locations: classpath:mapper/*Mapper.xml
1.3 启动类加注解
@SpringBootApplication
@MapperScan(basePackages = "com.ying.springboot01.mapper")
public class Springboot01Application {
public static void main(String[] args) {
SpringApplication.run(Springboot04Application.class, args);
}
}
1.4 编写service层代码
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> selectUsers() {
return userMapper.selectUsers();
}
}
1.编写controller层代码
@RestController
@RequestMapping("users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public List<User> selectUsers(){
return userService.selectUsers();
}
}
1.6 测试
http://localhost:8080/users
2 整合Druid数据源
2.1 导入依赖
<!-- druid起步依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
2.2 修改配置文件
# druid数据源配置
spring:
datasource:
druid:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///你的数据库
username: root
password: 1234
#初始化连接
initial-size: 20
#最大连接数量
max-active:
#最小空闲连接
min-idle: 10
#超时等待时间
max-wait: 00
2.3 Druid连接池后台监控
Druid连接池后台监控:http://localhost:8080/druid/index.html
3 整合单元测试
3.1 导入依赖
<!-- 单元测试起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>d
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
3.2 编写单元测试类
@RunWith(SpringRunner.class)
@SpringBootTest
class Springboot04ApplicationTests {
@Autowired
UserService userService;
@Test
public void testUserService(){
List<User> userList = userService.selectUsers();
userList.forEach(System.out::println);
}
}
4 整合 JSP
SpringBoot对JSP的支持不是很友好,所以JSP很少被使用
springboot 不支持jsp
但是需要了解
4.1 导入依赖
创建springboot-web工程
<!-- JSP核心引擎依赖 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!-- JSTL标签库(语法循环等等) -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
4.2 创建JSP页面
main目录下创建Java工程的
webapp
文件夹文章来源:https://www.toymoban.com/news/detail-460373.html从Controller转发到jsp文章来源地址https://www.toymoban.com/news/detail-460373.html
4.3 springmvc前缀和后缀配置
spring
mvc:
view:
prefix: /pages/
suffix: .jsp
4.4 编写Controller
@Controller
public class TestController {
@RequestMapping("test")
public String test(Model model){
model.addAttribute("msg","hello,jsp。");
return "index";
}
}
到了这里,关于SpringBoot -02 SpringBoot整合Mybatis、Druid数据源、单元测试、JSP的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!