SpringBoot学习笔记-03 从SSM到SpringBoot

1.集成MyBatis

A.添加依赖

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--继承父项目-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zzt</groupId>
    <artifactId>demo</artifactId>
    <version>SpringBoot-01</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--web开发-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--SpringBoot集成MyBatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>
        <!--Mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--SpringBoot测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!--SpringBoot打包-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <!--配置资源过滤-->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

</project>

B.SpringBoot配置文件

#数据库连接配置
spring.datasource.url = jdbc:mysql://localhost:3306/study_mybatis?serverTimezone=GMT%2B8
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
spring.datasource.username = ****
spring.datasource.password = ****
#引入mybatis主配置文件
mybatis.config-location=classpath:mybatis-config.xml
#引入mapper.xml文件(如果与dao接口处于同一包下可以不指定,用@MapperScan指定包路径)
#mybatis.mapper-locations=com/zzt/dao/*.xml

MyBatis主配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--实体类别名-->
    <typeAliases>
        <package name="com.zzt.vo"/>
    </typeAliases>
</configuration>

C.dao层(注意Mapper.xml需要与dao接口同名、@Mapper接口声明这是一个组件)

package com.zzt.mapper;

import com.zzt.vo.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface StudentMapper {
    public Student queryStudentById(@Param("id") int id);
}
<?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.zzt.mapper.StudentMapper">
    <select id="queryStudentById" resultType="Student">
        select id,name,age from t_stu where id=#{id}
    </select>
</mapper>

D.Service层(采用接口+实现类的方式)

package com.zzt.service;

import com.zzt.vo.Student;

public interface StudentService {
    public Student queryStudentById(Integer id);
}
package com.zzt.service.Impl;

import com.zzt.mapper.StudentMapper;
import com.zzt.service.StudentService;
import com.zzt.vo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    public Student queryStudentById(Integer id) {
        return studentMapper.queryStudentById(id);
    }
}

E.测试dao的注入(@MapperScan用于扫描Mapper.xml文件,适用于Mapper.xml与接口同包)

package com.zzt;

import com.zzt.service.Impl.StudentServiceImpl;
import com.zzt.service.StudentService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;


@SpringBootApplication()
@MapperScan("com.zzt.mapper") // 当mapper接口和xml文件处于同一包下,使用这种方式
public class DemoApplication {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(DemoApplication.class, args);
        System.out.println("容器创建成功");
        StudentService studentService = ctx.getBean(StudentServiceImpl.class);
        System.out.println(studentService.queryStudentById(1));
    }

}

6.执行结果

2.使用JSP展示数据

A.Controller

package com.zzt.controller;

import com.zzt.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/queryStudentById")
    public ModelAndView queryStudentById(@RequestParam("id") Integer id) {
        System.out.println("进入方法");
        ModelAndView mv = new ModelAndView();
        mv.addObject("student",studentService.queryStudentById(id));
        mv.setViewName("showStudent");
        return mv;
    }
}

B.JSP解析依赖

之前我们在做项目的时候,都是使用外部的Tomcat(Tomcat软件里集成了一些解析包)来解析JSP,而SpringBoot是内嵌的Tomcat,并没有默认包含用于解析JSP的包,因此我们还需要加入解析依赖:

        <!--内嵌Tomcat对JSP的解析包-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <!--jstl循环的依赖,按需要加入-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

C.视图解析器配置

#配置视图解析器 /表示webapp根目录
spring.mvc.view.prefix=/ 
spring.mvc.view.suffix=.jsp

D.新建webapp,配置编译时包含webapp资源

            <resource>
                <directory>src/main/webapp</directory>
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>

E.JSP页面 /showStudent.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String basePath = request.getScheme()+ "://" +
            request.getServerName() + ":" + request.getServerPort() +
            request.getContextPath() + "/";
%>
<html>
<head>
    <title>标题</title>
    <base href="<%=basePath%>" />
</head>
<body>
    ${student.id}--${student.name}--${student.age}
</body>
</html>

F.启动项目,访问地址:http://localhost:8080/student/queryStudentById?id=1

3.热部署插件JRebel-解决项目修改时需要重启的问题

方式一:搜索自动安装(会比较慢,有梯子可尝试)

方式二:下载压缩包,解压在ideal的plugins目录下

需要激活,可参考:http://www.cicoding.cn/other/jrebel-activation/(可能还是需要梯子)

现在我们使用Rebel启动项目:

若代码发生了改变,我们只需要重新build一下即可。

[注]:如果觉得XRebel(与JRebel绑定下载的)激活弹窗很麻烦,也可以选择激活它~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值