springBoot整合ssm

新建不使用骨架的maven工程,项目结构图如图

springBoot整合ssm_第1张图片

 最后效果:

springBoot整合ssm_第2张图片

 MySql脚本:

MySQL - 5.5.40 
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;

create table `user` (
    `id` int (11),
    `username` varchar (60),
    `password` varchar (60),
    `name` varchar (60),
    `age` int (11),
    `sex` char (3),
    `birthday` date ,
    `created` date ,
    `updated` date 
); 
insert into `user` (`id`, `username`, `password`, `name`, `age`, `sex`, `birthday`, `created`, `updated`) values('1','aaa','aaaa','aaa','20','','2020-04-01','2020-04-02','2020-04-03');
insert into `user` (`id`, `username`, `password`, `name`, `age`, `sex`, `birthday`, `created`, `updated`) values('2','bbb','bbbb','bbb','21','','2020-04-08','2020-04-09','2020-04-10');

 

 

 

 pom.xml文件

 1 xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0modelVersion>
 6 
 7     <groupId>org.examplegroupId>
 8     <artifactId>springbootartifactId>
 9     <version>1.0-SNAPSHOTversion>
10     
11     <parent>
12         <groupId>org.springframework.bootgroupId>
13         <artifactId>spring-boot-starter-parentartifactId>
14         <version>2.0.6.RELEASEversion>
15     parent>
16 
17     <dependencies>
18         
19         <dependency>
20             <groupId>org.springframework.bootgroupId>
21             <artifactId>spring-boot-starter-webartifactId>
22         dependency>
23 
24         
25         <dependency>
26             <groupId>org.springframework.bootgroupId>
27             <artifactId>spring-boot-starter-jdbcartifactId>
28         dependency>
29         
30         <dependency>
31             <groupId>mysqlgroupId>
32             <artifactId>mysql-connector-javaartifactId>
33         dependency>
34         
35         <dependency>
36             <groupId>org.mybatis.spring.bootgroupId>
37             <artifactId>mybatis-spring-boot-starterartifactId>
38             <version>1.3.2version>
39         dependency>
40         
41         <dependency>
42             <groupId>tk.mybatisgroupId>
43             <artifactId>mapper-spring-boot-starterartifactId>
44             <version>2.0.2version>
45         dependency>
46 
47         
48         <dependency>
49             <groupId>org.springframework.bootgroupId>
50             <artifactId>spring-boot-starter-thymeleafartifactId>
51         dependency>
52     dependencies>
53 
54 
55 
56 project>

 

application.properties配置文件:

#端口号
server.port=80
#logging.level.org.springframework=debug
# 连接四大参数
spring.datasource.url=jdbc:mysql://192.168.2.137:3306/ssm
spring.datasource.username=root
spring.datasource.password=1234
# 可省略,SpringBoot自动推断
spring.datasource.driverClassName=com.mysql.jdbc.Driver

spring.datasource.hikari.idle-timeout=60000
spring.datasource.hikari.maximum-pool-size=30
spring.datasource.hikari.minimum-idle=10

#mybatis要扫描的实体类包
mybatis.type-aliases-package=com.hao.pojo
#禁止模板缓存,开发阶段可以关掉缓存使用
spring.thymeleaf.cache=false

 

引导类:

package com.hao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class UserApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class);
    }
}

 

实体类User:

  1 package com.hao.pojo;
  2 
  3 import javax.persistence.GeneratedValue;
  4 import javax.persistence.GenerationType;
  5 import javax.persistence.Id;
  6 import javax.persistence.Table;
  7 import java.util.Date;
  8 
  9 @Table(name = "user")
 10 public class User {
 11     @Id
 12     @GeneratedValue(strategy = GenerationType.IDENTITY)
 13     private Integer id;
 14 
 15     private String username;
 16 
 17     private String password;
 18 
 19     private String name;
 20 
 21     private Integer age;
 22 
 23     private Character sex;
 24 
 25     private Date birthday;
 26 
 27     private Date created;
 28 
 29     private Date updated;
 30 
 31     public Integer getId() {
 32         return id;
 33     }
 34 
 35     public void setId(Integer id) {
 36         this.id = id;
 37     }
 38 
 39     public String getUsername() {
 40         return username;
 41     }
 42 
 43     public void setUsername(String username) {
 44         this.username = username;
 45     }
 46 
 47     public String getPassword() {
 48         return password;
 49     }
 50 
 51     public void setPassword(String password) {
 52         this.password = password;
 53     }
 54 
 55     public String getName() {
 56         return name;
 57     }
 58 
 59     public void setName(String name) {
 60         this.name = name;
 61     }
 62 
 63     public Integer getAge() {
 64         return age;
 65     }
 66 
 67     public void setAge(Integer age) {
 68         this.age = age;
 69     }
 70 
 71     public Character getSex() {
 72         return sex;
 73     }
 74 
 75     public void setSex(Character sex) {
 76         this.sex = sex;
 77     }
 78 
 79     public Date getBirthday() {
 80         return birthday;
 81     }
 82 
 83     public void setBirthday(Date birthday) {
 84         this.birthday = birthday;
 85     }
 86 
 87     public Date getCreated() {
 88         return created;
 89     }
 90 
 91     public void setCreated(Date created) {
 92         this.created = created;
 93     }
 94 
 95     public Date getUpdated() {
 96         return updated;
 97     }
 98 
 99     public void setUpdated(Date updated) {
100         this.updated = updated;
101     }
102 
103     @Override
104     public String toString() {
105         return "User{" +
106                 "id=" + id +
107                 ", username='" + username + '\'' +
108                 ", password='" + password + '\'' +
109                 ", name='" + name + '\'' +
110                 ", age=" + age +
111                 ", sex=" + sex +
112                 ", birthday=" + birthday +
113                 ", created=" + created +
114                 ", updated=" + updated +
115                 '}';
116     }
117 }

 

UserMapper:

package com.hao.mappers;

import com.hao.pojo.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper extends tk.mybatis.mapper.common.Mapper{
}

 

UserService:

 1 package com.hao.service;
 2 
 3 import com.hao.mappers.UserMapper;
 4 import com.hao.pojo.User;
 5 
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Service;
 8 import org.springframework.transaction.annotation.Transactional;
 9 
10 import java.util.List;
11 
12 @Service
13 public class UserService {
14 
15     @Autowired
16     private UserMapper userMapper;
17 
18     public User queryById(Integer id){
19         User user = this.userMapper.selectByPrimaryKey(id);
20         return this.userMapper.selectByPrimaryKey(id);
21     }
22 
23     @Transactional
24     public void deleteById(Integer id){
25         this.userMapper.deleteByPrimaryKey(id);
26     }
27 
28     public List queryAll() {
29         List users = this.userMapper.selectAll();
30         return this.userMapper.selectAll();
31     }
32 }

 

UserController:

package com.hao.controller;

import com.hao.pojo.User;
import com.hao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//@RestController
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
/*
    @GetMapping("{id}")
    public User queryUserById(@PathVariable("id")Integer id){
        return this.userService.queryById(id);
    }*/

    @GetMapping("hello")
    public String test(){
        return "hello ssm";
    }

    @GetMapping("all")
    public String all(ModelMap model) {
        // 查询用户
        List users = this.userService.queryAll();
        // 放入模型
        model.addAttribute("users", users);
        // 返回模板名称(就是classpath:/templates/目录下的html文件名)
        return "users";
    }
}

 

users.html模板:

 1 DOCTYPE html>
 2 <html xmlns:th="http://www.thymeleaf.org">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>首页title>
 6     <style type="text/css">
 7         table {border-collapse: collapse; font-size: 14px; width: 80%; margin: auto}
 8         table, th, td {border: 1px solid darkslategray;padding: 10px}
 9     style>
10 head>
11 <body>
12 <div style="text-align: center">
13     <span style="color: darkslategray; font-size: 30px">欢迎光临!span>
14     <hr/>
15     <table class="list">
16         <tr>
17             <th>idth>
18             <th>姓名th>
19             <th>用户名th>
20             <th>年龄th>
21             <th>性别th>
22             <th>生日th>
23         tr>
24         <tr th:each="user : ${users}">
25             <td th:text="${user.id}">1td>
26             <td th:text="${user.name}">张三td>
27             <td th:text="${user.username}">zhangsantd>
28             <td th:text="${user.age}">20td>
29             <td th:text="${user.sex}">td>
30             <td th:text="${user.birthday}">1980-02-30td>
31         tr>
32     table>
33 div>
34 body>
35 html>

 

自定义拦截器:

 1 package com.hao.interceptors;
 2 
 3 import org.springframework.stereotype.Component;
 4 import org.springframework.web.servlet.HandlerInterceptor;
 5 import org.springframework.web.servlet.ModelAndView;
 6 
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 @Component
11 public class MyInterceptor implements HandlerInterceptor {
12 
13     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
14         System.out.println("preHandle method is running!");
15         return true;
16     }
17 
18     public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
19         System.out.println("postHandle method is running!");
20     }
21 
22     public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
23         System.out.println("afterCompletion method is running!");
24     }
25 }

 

注册拦截器:

 1 package com.hao.config;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.context.annotation.Configuration;
 5 import org.springframework.web.servlet.HandlerInterceptor;
 6 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
 7 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 8 
 9 @Configuration
10 class MvcConfiguration implements WebMvcConfigurer {
11 
12     @Autowired
13     private HandlerInterceptor myInterceptor;
14     /**
15      * 重写接口中的addInterceptors方法,添加自定义拦截器
16      * @param registry
17      */
18     @Override
19     public void addInterceptors(InterceptorRegistry registry) {
20         registry.addInterceptor(myInterceptor).addPathPatterns("/**");
21     }
22 }

 

你可能感兴趣的:(springBoot整合ssm)