SpringBoot入门小项目 springboot+mybatis+mysql+thymeleaf实现数据库操作并展示数据
2019-03-27 16:35
971 查看
SpringBoot入门小项目springboot+mybatis+mysql+thymeleaf实现数据库操作并展示数据
- 1. springboot的demo项目快速部署下载
- 2. MySQL安装下载
- 3. 代码
- 3.1 依赖
- 3.2 配置文件
- 3.3 项目结构
- 3.4 代码内容
- 3.4.1 实体类Person.java
- 3.4.2 mybatis配置 PersonMapper.xml
- 3.4.2 mybatis注解实现 PersonMapper.java
- 3.4.3 利用三层方式访问数据库并显示
1. springboot的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 项目结构
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 < 4000 span class="token string">"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操纵数据库有个概念。
相关文章推荐
- springBoot2.0整合myBatis(mysql)实现基本数据库操作
- Java Web学习系列——Maven Web项目中集成使用Spring、MyBatis实现对MySQL的数据访问
- SpringBoot入门-17(springboot集成mybatis注解形式实现ID自动增长)
- springboot整合mybatis和mysql实现数据的增删改查
- springboot+mybatis+easyui 前台实现数据库信息的增删改操作
- Spring Boot快速入门(五):使用MyBatis(注解形式)进行数据库操作
- SpringBoot项目 使用Sprin Data Jpa 实现数据库的 增 ,删, 改, 查操作
- springboot+jpa+hibernate+idea做个实现数据库mysql增数据乱码,中文乱码为???
- SpringBoot项目中使用MongoDB充当数据库以及进行数据的增删查改的操作
- Spring+Mybatis 实现aop数据库读写分离与多数据库源配置操作
- springboot学习(4)springboot使用thymeleaf完成数据的页面展示
- Spring Boot2.0.3 Mybatis Sharding-jdbc3.X集成实现数据库的读写分离(二)-项目实现
- SpringBoot2.0完美整合Mybatis并使用thymeleaf展示数据
- 基于ExtJs6前台,SpringMVC-Spring-Mybatis,resteasy,mysql无限极表设计,实现树状展示数据(treepanel)
- springboot——mybatis+mysql+thymeleaf开发一个restful服务,实现用户增删改(完整版)
- Spring Boot2.0.3 Mybatis Sharding-jdbc3.X集成实现数据库的读写分离(一)MySql的主从配置
- 详细步骤!!!idea+springboot+mybatis+jsp+bootstrap实现从mysql查询出数据并显示(源码)
- springboot+mybatis+springmvc+mysql简单项目
- Maven + Spring MVC+Mybatis + MySQL +AngularJS + Bootstrap 实现简单微博应用(二)访问数据库
- Spring boot整合mybatis实现数据的CRUD