SpringBoot入门小项目 springboot+mybatis+mysql+thymeleaf实现数据库操作并展示数据

1. springboot的demo项目快速部署下载

官方demo项目快速部署

2. MySQL安装下载

此处不做描述

3. 代码

3.1 依赖

dependencies {
	compile('org.springframework.boot:spring-boot-starter-web')
	testCompile('org.springframework.boot:spring-boot-starter-test')
	compile('org.springframework.boot:spring-boot-starter')
	
	compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.1.1')
	compile('mysql:mysql-connector-java')
	compile 'net.sourceforge.nekohtml:nekohtml:1.9.12'
	compile 'org.springframework.boot:spring-boot-starter-thymeleaf:1.5.9.RELEASE'
}

mybatis依赖+mysql数据库连接池

	compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.1.1')
	compile('mysql:mysql-connector-java')

thymeleaf插件
(nekohtml能够有效的避免html页面中缺少/引起的报错)

	compile 'net.sourceforge.nekohtml:nekohtml:1.9.12'
	compile 'org.springframework.boot:spring-boot-starter-thymeleaf:1.5.9.RELEASE'

3.2 配置文件

server.port= 8070

#数据库连接(useSSL=true能够有效的避免mysql版本过高引起的报错)
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useSSL=true&verifyServerCertificate=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#配置mybatis需要扫描的包
mybatis.typeAliasesPackage=com.example.demo.entity
mybatis.mapperLocations=classpath:mapper/*Mapper.xml

#配置thymeleaf要扫描的html所在的包
spring.thymeleaf.prefix=classpath:/static
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=LEGACYHTML5

3.3 项目结构

SpringBoot入门小项目 springboot+mybatis+mysql+thymeleaf实现数据库操作并展示数据

3.4 代码内容

3.4.1 实体类Person.java

这边省略了set和get(这个是很小的demo 偷懒了)

package com.example.demo.entity;

public class Person {

    private int id;
    private String account;
    private String password;

    @Override
    public String toString(){
        return "Person{" +
                "id=" + id +
                ",account=" + account +
                ",password=" + password + "}";
    }
}

3.4.2 mybatis配置 PersonMapper.xml

这边使用xml方式配置,后边讲用注解也可以直接写sql语句

<?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" >
<!--你接口的包名是com.abc.dao,接口名是NameMapper.java,那么你的mapper.xml的namespace应该是com.abc.dao.NameMapper-->
<mapper namespace="com.example.demo.dao.PersonDao" >

    <!--resultMap对应的是表与实体类的映射  - type 数据库表对应的实体类,别名或完整类名都可以-->
    <resultMap id="BaseResultMap" type="com.example.demo.entity.Person" >
        <!-- 结果集的主键 -->
        <id column="id" property="id" jdbcType="INTEGER" />
        <!-- 普通的列  -column 是数据库中字段, property是实体类中字段-->
        <result column="account" property="account" jdbcType="VARCHAR" />
        <result column="password" property="password" jdbcType="VARCHAR" />
    </resultMap>


    <!--parameterType(输入类型)、resultType(输出类型)-->
    <select id="getById" parameterType="int" resultMap="BaseResultMap" resultType="com.example.demo.entity.Person">

        SELECT * FROM login WHERE id = #{id,jdbcType=INTEGER}
    </select>

</mapper>

3.4.2 mybatis注解实现 PersonMapper.java

直接利用注解写sql语句,和xml选1即可

package com.example.demo.mapper;

import com.example.demo.entity.Person;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface PersonMapper {
    //直接利用接口里面的标注方式写sql语句
    @Select("SELECT * FROM login WHERE id = #{id}")
    Person getById(@Param("id") int id);
}

3.4.3 利用三层方式访问数据库并显示

这个例子省略了service层的调用

PersonDao.java

package com.example.demo.dao;

import com.example.demo.entity.Person;

public interface PersonDao {
    Person getById(int id);
}

MyController.java

package com.example.demo.controller;

import com.example.demo.dao.PersonDao;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

//@RestController
@Controller
public class MyController {
    @Autowired
    //private PersonMapper personMapper;
    private PersonDao personDao;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    @ResponseBody
    public String helloWorld(){
        //return this.personMapper.getById(1).toString();
        return this.personDao.getById(1).toString();
    }
    @ResponseBody
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public String postHttp(){
        return "http";
    }
    @RequestMapping("/index")
    public String index(Model model){
        model.addAttribute("page","aaaaa");
        return "/index";
    }
}

index.html(注意thymeleaf的使用)

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<a href="" id="aa">aaa</a>
<span th:text="${page}"></span>
</body>
</html>

最后是启动类了(要添加注解,扫描对应的包)

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.demo.dao")// mybatis扫描路径,针对的是接口Mapper类
public class DemoApplication {
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

4. 总结

能够成功运行啦!主要是对最基本的springboot操纵数据库有个概念。