java学习-springboot

新建一个springboot项目:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


@RestController
public class Hellospringboot {
    @RequestMapping("/hello")
    public String test() {
        return "Hello SpringBoot";
    }
}

在这里插入图片描述
在这里插入图片描述
springboot整合mybatis
导入需要的依赖(这些只是我们另外导入的部分依赖文件)

<!-- 这是自定义的包 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>
        <!-- mysql 驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!--依赖-->
        <!--在这里写的依赖都不需要版本号,只要在父依赖中有!-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

编写配置:在下面的两个文件中都可以进行配置编写
在这里插入图片描述
application.properties

# 在这里也可以编写一些SpringBoot的配置
# 更改 tomcat 端口号
server.port=9090
# 一会集成 MyBatis 也在这里配置

#spring.datasource.username=root
#spring.datasource.password=123456
#spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

application.yml

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver

springboot默认的是hikari数据源
在测试类中测试是否连接
在这里插入图片描述

@SpringBootTest
class SpringbootMybatisApplicationTests {

    @Autowired
    private DataSource dataSource;
    @Test
    void contextLoads() throws SQLException {
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
    }

}

创建一个pojo/User类

@Data
public class User {
    private int id;
    private String name;
    private String pwd;
}

创建mapper或者叫dao层
在这里插入图片描述

UserMapper

@Mapper // @Mapper 标注这是一个Mapper接口
@Repository // 代表持久层
public interface UserMapper {

    // @Select("select * from user")
    List<User> getUserLists();

}

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.kuang.mapper.UserMapper">
   <select id="getUserLists" resultType="User">
       select * from user;
   </select>
</mapper>

还需要配置application.yml

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:
  type-aliases-package: com.gg.pojo
  mapper-locations: classpath:com/gg/mapper/*.xml

控制层
在这里插入图片描述

@RestController
public class MybatisController {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/get")
    public String getUserList(){
        List<User> userLists = userMapper.getUserLists();

        return String.valueOf(userLists);

    }
}

运行测试

springboot-web
新建项目
在这里插入图片描述

在static资源目录下 可以直接访问(如果没有controller控制会直接访问,若有则会访问跳转页面)
在templates资源目录下 要想访问需要使用controller进行跳转(首先需要spring-boot-starter-thymeleaf依赖)

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

IndexController

@Controller
public class IndexController {
    @RequestMapping("/")
    public String index(){
        return "index";
    }
    @RequestMapping("/user")
    public String index1(){
        return "user/index";
    }
}

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>user首页</h1>
</body>
</html>

传值

先导入头文件:<html lang="en" xmlns:th="http://www.thymeleaf.org">

@Controller
public class IndexController {
    @RequestMapping("/")
    public String index(Model model){

        model.addAttribute("msg","nihao");

        model.addAttribute("users", Arrays.asList("guojian","guojian1","guojian2"));
        return "index";
    }
    @RequestMapping("/user")
    public String index1(){
        return "user/index";
    }
}

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>templates首页</h1>
<p th:text="${msg}"></p>
<-- 三种遍历的写法-->
<p th:each="user:${users}" th:text="${user}"/>

<h1 th:each="user:${users}">[[${user}]]</h1>

<div th:each="user:${users}">
    <p th:text="${user}"/>
</div>

</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值