创建一个 Spring Initializr项目 参照 : 第一个Springboot
- 本文不使用application.properties文件 而使用更加简洁的application.yml文件。将resource文件夹下原有的application.properties文件删除,创建application.yml配置文件(备注:其实SpringBoot底层会把application.yml文件解析为application.properties),本文创建了两个yml文件(application.yml和application-dev.yml),分别来看一下内容
application.yml
spring:
profiles:
active: dev
application-dev.yml
server:
port: 8080
spring:
datasource:
username: root
password: 333
url: jdbc:mysql://localhost:3306/smbms
driver-class-name: com.mysql.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.example.ssm.entity
- 创建 对应的表,(用自己的就好)
- 创建包controller,entity,service,dao(习惯把数据层叫dao)。resources下创建mapper 文件夹,用于写sql语句;
- 直接贴代码
User.java (记得get set )
public class User {
private int id;
private String userCode;
private String userName;
private String userPassword;
}
UserDao.java
@Repository
public interface UserDao {
List<User> loadAll();
}
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.example.ssm.dao.UserDao">
<select id="loadAll" resultType="com.example.ssm.entity.User">
select * from smbms_user
</select>
</mapper>
UserService.java
@Service
public class UserService {
@Autowired
private UserDao userDao;
public List<User> loadAll(){
return userDao.loadAll();
}
}
UserController.java
@Controller
public class MasterController {
@Autowired
private UserService us;
@ResponseBody //看情况加(加了直接输入,不加跳转到Hello,SpringBoot.html)
@RequestMapping("hello")
public String hello(){
System.out.println("进");
List<User> users = us.loadAll();
for (User u:users) {
System.out.println(u.toString());
}
return "Hello,SpringBoot";
}
- 框架完成目录
最后再给启动类 下面在 启动类 里加上注解用于给出需要扫描的 dao文件路径 (启动类路径一定要比其他类高一级)@MapperScan(“com.example.ssm.dao”)
@MapperScan("com.example.ssm.dao") @SpringBootApplication public class SsmApplication { public static void main(String[] args) { SpringApplication.run(SsmApplication.class, args); } }
最后浏览器输入
控制台展示
进 User{id=1, userCode='admin', userName='系统管理员', userPassword='1234567'} User{id=2, userCode='liming', userName='我', userPassword='0000000'} User{id=5, userCode='hanlubiao', userName='韩路彪', userPassword='0000000'} User{id=6, userCode='zhanghua', userName='张华', userPassword='0000000'} User{id=7, userCode='wangyang', userName='王洋', userPassword='0000000'} User{id=8, userCode='zhaoyan', userName='赵燕', userPassword='0000000'} User{id=10, userCode='sunlei', userName='孙磊', userPassword='0000000'} User{id=11, userCode='sunxing', userName='孙兴', userPassword='0000000'} User{id=12, userCode='zhangchen', userName='张晨', userPassword='0000000'} User{id=13, userCode='dengchao', userName='邓超', userPassword='0000000'} User{id=14, userCode='yangguo', userName='杨过', userPassword='0000000'} User{id=15, userCode='zhaomin', userName='赵敏', userPassword='0000000'} User{id=16, userCode='liubo', userName='刘波', userPassword='00000000'} User{id=17, userCode='liubo', userName='刘波', userPassword='00000000'} User{id=27, userCode='null', userName='null', userPassword='null'} User{id=28, userCode='null', userName='null', userPassword='null'}
尝试加入SpringMVC
application-dev.yml 中加入模板代码块;
pom.xml 中添加 前端模板,顺便修改mysql驱动(我是改了,不然版本有点高启动不了)
修改controller
templates目录下添加index.html
application-dev.yml
server:
port: 8080
spring:
datasource:
username: root
password: 333
url: jdbc:mysql://localhost:3306/smbms
driver-class-name: com.mysql.jdbc.Driver
thymeleaf:
prefix: classpath:/templates/
suffix: .html
mode: HTML5
encoding: UTF-8
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.example.ssm.entity
pom.xml
<dependency> <!-- 需要修改的 -->
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency> <!-- 添加的 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Controller.java
@Controller
public class MasterController {
@Autowired
private UserService us;
@RequestMapping("hello")
public String hello(Model mo){
System.out.println("进");
List<User> users = us.loadAll();
for (User u:users) {
System.out.println(u.toString());
}
mo.addAttribute("u",users);
return "index";
}
index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"> <!-- 注意加th -->
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<table width="500px" >
<tr>
<td>ID</td>
<td>姓名</td>
<td>密码</td>
<td>电话</td>
</tr>
<tr th:each="us : ${u}" >
<td th:text="${us.id}"></td>
<td th:text="${us.userName}"></td>
<td th:text="${us.userPassword}"></td>
<td th:text="${us.userCode}"></td>
</tr>
</table>
</body>
</html>
- 启动后效果
以上就是全部内容