笔者职场菜鸟,但是希望通过些博客,记录自己的学习过程,以后有需要可以直接拿来用。谁说拿来主义不好。 也希望能帮助到一些朋友。
对自己有用的博客就是号博客,比如简单的Ctrl+alt+m。 可以让你的开发效率提升很多。
1、项目结构
2、pom.xml
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis的依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!--数据库的依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--druid数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--thymeleaf模版-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
说明:主要是mybatis,和mysql数据的依赖,以及druid数据
thymeleaf模版引擎
3、application-dev.yml的配置
server:
port: 8091
# 数据源和mybatis的相关配合
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
# thymeleaf静态资源配置
thymeleaf:
prefix: classpath:/templates/
suffix: .html
mode: HTML5
encoding: UTF-8
servlet:
content-type: text/html
cache: false
mybatis:
mapper-locations: classpath*:mapper/*Mapper.xml
#起别名。可省略写mybatis的xml中的resultType的全路径
type-aliases-package: com.jxq.ssm.pojo
#控制台打印sql语句
logging:
level:
com.jxq.ssm.mapper: debug
application.yml
spring:
profiles:
active: dev
注: 笔记:在Spring
Boot中多环境配置文件名需要满足application-{profile}.yml的格式,其中{profile}对应你的环境标识,比如:application-dev.yml:开发环境 application-test.yml:测试环境
application-prod.yml:生产环境
说明:数据库的配置需要加入,配置mybatis的别名
?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
防止乱码,以及加入时区
4、业务编码:
pojo.java
public class User {
private Integer id;
private String username;
private String password;
...
省略setter,getter方法...
mapper.java
@Mapper
public interface UserMapper {
List<User> findAll();
}
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">
<!--nameapce:名称空间,用于隔离sql语句,必须是所对应Mapper接口的全路径名(包名.接口名)-->
<mapper namespace="com.jxq.ssm.mapper.UserMapper">
<select id="findAll" resultType="User">
select id,username,password
from user
</select>
</mapper>
serviceImpl.java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.findAll();
}
}
controller.java
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/findAll")
public String findAll(Model model) {
List<User> userList = userService.findAll();
model.addAttribute("userList",userList);
return "userList";
}
}
说明:使用@RestController,返回的是字符串,不是页面.
5、userlist.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml" xmlns:method="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>用户信息展现</title>
</head>
<body>
<table>
<tr>
<th>id</th>
<th>用户名</th>
<th>密码</th>
</tr>
<tr th:each="user:${userList}"><!--循环userList,以user作为形参-->
<td th:text="${user.id}"></td>
<td th:text="${user.username}"></td>
<td th:text="${user.password}"></td>
</tr>
</table>
</body>
</html>
6、测试结果
测试通过,springboot整合ssm,比传统方式整合简单了很多。 希望,写些简单的列子帮助自己记忆,也给于想学习java的朋友一些参考。
有不对的地方大家指出,基本按着步骤都是可以搭建起来环境。很多的博客写的很好,但是不适合 我们这种新手。哈哈,偶尔写写文章感觉还是不错。