本文主要是介绍spring boot项目创建,集成mybatis配置mysql连接,编写测试代码完成访问接口查询数据库,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
springboot项目创建mybatis配置mysql,完成查询数据库测试
准备工作:
1.去https://start.spring.io/创建项目,本例使用的是 springboot 2.4.0-SNAPSHOT ,Maven,JAVA 8,依赖WEB MVC。
2.JAVA开发工具导入Maven项目,本例使用的是eclipse。
3.resources文件夹下创建mapper文件夹备用
4.本地要有jdk8环境便于测试
一.首先引入mysql和mybatis的jar包
<!--mysql数据库驱动--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><!--mybatis--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.0</version></dependency>
二.再resources文件夹下创建application.yml和mybatis-config.xml配置文件
1.application.yml 配置端口,数据库配置,mybatis配置
server:port: 9898
spring:#数据库连接配置datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://IP:3306/test?characterEncoding=utf-8&useSSL=falseusername: 账号password: 密码#mybatis的相关配置
mybatis:#mapper配置文件mapper-locations: classpath:mapper/*.xmltype-aliases-package: cn.richwit.blog.entityconfig-location: classpath:mybatis-config.xml
2.mybatis-config.xml(空文件什么不配置也要创建)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><!--以下已经在application.yml中配置过了--><!--<typeAliases>--><!--<package name="com.example.domain"/>--><!--</typeAliases>--><!--<mappers>--><!--<mapper resource="mapper/UserMapper.xml"/>--><!--</mappers>-->
</configuration>
三.编写测试代码
1.entity实体类
package cn.richwit.blog.entity;import java.util.Date;/**
* @version 1.0
*/
public class Article {//idprivate int article_id;//标题private String article_title;//内容private String article_comtent;//类型private String article_type;//用户idprivate int user_id;//阅读量private long reading_quantity;private Date create_time;private Date update_time;public Article() {super();}public Article(int article_id, String article_title, String article_comtent, String article_type, int user_id,long reading_quantity, Date create_time, Date update_time) {super();this.article_id = article_id;this.article_title = article_title;this.article_comtent = article_comtent;this.article_type = article_type;this.user_id = user_id;this.reading_quantity = reading_quantity;this.create_time = create_time;this.update_time = update_time;}public int getArticle_id() {return article_id;}public void setArticle_id(int article_id) {this.article_id = article_id;}public String getArticle_title() {return article_title;}public void setArticle_title(String article_title) {this.article_title = article_title;}public String getArticle_comtent() {return article_comtent;}public void setArticle_comtent(String article_comtent) {this.article_comtent = article_comtent;}public String getArticle_type() {return article_type;}public void setArticle_type(String article_type) {this.article_type = article_type;}public int getUser_id() {return user_id;}public void setUser_id(int user_id) {this.user_id = user_id;}public long getReading_quantity() {return reading_quantity;}public void setReading_quantity(long reading_quantity) {this.reading_quantity = reading_quantity;}public Date getCreate_time() {return create_time;}public void setCreate_time(Date create_time) {this.create_time = create_time;}public Date getUpdate_time() {return update_time;}public void setUpdate_time(Date update_time) {this.update_time = update_time;}}
2.controller控制层
package cn.richwit.blog.controller;import java.util.HashMap;
import java.util.Map;import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.richwit.blog.entity.Article;
import cn.richwit.blog.service.ArticleService;/**
* @version 1.0
*/
@RestController
@RequestMapping("/article")
public class ArticleController {@Autowiredprivate ArticleService articleService;@RequestMapping("/getarticlebyid")public Map<String,Object> getArticleById(String articleId){Map<String,Object> map = new HashMap<>();if(StringUtils.isBlank(articleId)) {map.put("err", "错误请求!");return map;}Article article = articleService.getArticleById(Integer.parseInt(articleId));if(article == null) {map.put("success", false);}else {map.put("success", true);map.put("article", article);}return map;}}
3.service服务层
接口
package cn.richwit.blog.service;import cn.richwit.blog.entity.Article;/**
* @version 1.0
*/
public interface ArticleService {Article getArticleById(int id);}
实现类
package cn.richwit.blog.service;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import cn.richwit.blog.dao.ArticleDao;
import cn.richwit.blog.entity.Article;/** * @version 1.0
*/
@Service
public class ArticleServiceImpl implements ArticleService{@Autowiredprivate ArticleDao articleDao;@Overridepublic Article getArticleById(int id) {Article article = articleDao.findArticleById(id);return article;}}
4.dao数据层
package cn.richwit.blog.service;import java.util.HashMap;
import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import cn.richwit.blog.dao.ArticleDao;
import cn.richwit.blog.entity.Article;/**
* @version 1.0
*/
@Service
public class ArticleServiceImpl implements ArticleService{@Autowiredprivate ArticleDao articleDao;@Overridepublic Map<String, Object> getArticleById(int id) {Map<String, Object> map = new HashMap<>();Article article = articleDao.findArticleById(id);map.put("article", article);return map;}}
5.dao接口对应的mapper
<?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="cn.richwit.blog.dao.ArticleDao"><select id="findArticleById" parameterType="int" resultType="Article">SELECT * FROM Article where article_id = #{articleId}</select>
</mapper>
6.最后再启动类上加上扫描dao接口
package cn.richwit.blog;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan("cn.richwit.blog.dao")
public class BlogApplication {public static void main(String[] args) {SpringApplication.run(BlogApplication.class, args);}}
7.创建实体类对应的数据库表,并插入一条测试数据
最后运行启动类
访问:http://127.0.0.1:9898/article/getarticlebyid?articleId=1
这篇关于spring boot项目创建,集成mybatis配置mysql连接,编写测试代码完成访问接口查询数据库的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!