Spring_Boot

目录:

POM.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ali</groupId>
    <artifactId>springBoot</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.7.RELEASE</version>
    </parent>


    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--  <dependency>
              <groupId>com.alibaba</groupId>
              <artifactId>druid</artifactId>
              <version>1.1.6</version>
          </dependency>-->

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!--mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

    </dependencies>


</project>

Application启动类:


@SpringBootApplication
@MapperScan(basePackages = "com.ali.mapper")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

pojo:


public class TbDept implements Serializable {

    private Integer id;
    private String name;
    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

mapper:



@Repository
public interface DeptMapper {

    List<TbDept> findList();
}

service:


@Service
public class DeptService {


    @Autowired
    private DeptMapper deptMapper;

    public List<TbDept>  findList(){

        return deptMapper.findList();
    }

}

controller:

@Controller
public class DemoController {


    @Autowired
    private DeptService deptService;


    @RequestMapping("findList")
    @ResponseBody
    public List<TbDept> findList(){


        return deptService.findList();
    }

    @RequestMapping("findAll")
    public String findAll(ModelMap modelMap){

        List<TbDept> list = deptService.findList();

        modelMap.addAttribute("list",list);

        modelMap.addAttribute("msg","hello");

        return "haha";
    }

}

拦截器:

public class LoginInterceptor implements HandlerInterceptor {

    private Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);


    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        logger.debug("preHandle method is now running!");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        logger.debug("postHandle method is now running!");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        logger.debug("afterCompletion method is now running!");
    }
}

config:


@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Bean
    public LoginInterceptor loginInterceptor(){

        return new LoginInterceptor();

    }

    //添加拦截器
    public void addInterceptors(InterceptorRegistry registry) {
        // 通过registry来注册拦截器,通过addPathPatterns来添加拦截路径
        registry.addInterceptor(this.loginInterceptor()).addPathPatterns("/**");
    }

}

resource中的资源:

mappers:

DeptMapper.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.ali.mapper.DeptMapper">

    <select id="findList" resultType="tbDept">

        SELECT
          *
          FROM  dept

    </select>

</mapper>

 

templates:中主要放置themleaf的html页面:

<!DOCTYPE html>
//其中要有xmlns:th="http://www.thymeleaf.org"
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>haha</title>
</head>
<body>


<h1 th:text="${msg}">我是h1</h1>

<table table border="1">

    <tr th:each="dept : ${list}">

        <td th:text="${dept.id}">1</td>
        <td th:text="${dept.name}">admin</td>
        <td th:text="${dept.age}">88</td>


    </tr>
</table>
</body>
</html>

application.properties:为springBoot默认使用的配置文件

#修改端口
server.port=9001

# 连接四大参数
spring.datasource.url=jdbc:mysql://localhost:3306/miu
spring.datasource.username=root
spring.datasource.password=root
# 可省略,SpringBoot自动推断
spring.datasource.driverClassName=com.mysql.jdbc.Driver

#设置日志级别
logging.level.com.ali=debug

# mybatis 别名扫描
mybatis.type-aliases-package=com.ali.pojo
# mapper.xml文件位置,如果没有映射文件,请注释掉
mybatis.mapper-locations=classpath:mappers/*.xml

#禁用thyemelaaf 模板缓存
spring.thymeleaf.cache=false

static中存放静态图片

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值