这篇文章将为大家详细讲解有关Spring Boo如何t连接MySQL数据库,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
湖南网站建设公司创新互联,湖南网站设计制作,有大型网站制作公司丰富经验。已为湖南近千家提供企业网站建设服务。企业网站搭建\外贸营销网站建设要多少钱,请找那个售后服务好的湖南做网站的公司定做!
Spring Boot有两种方法与数据库建立连接,一种是使用JdbcTemplate,另一种集成Mybatis,下面分别为大家介绍一下如何集成和使用这两种方式。
1. 使用JdbcTemplate
mysql mysql-connector-java org.springframework.boot spring-boot-starter-jdbc
在resource文件夹下添加application.properties配置文件并输入数据库参数,内容如下:
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.max-idle=10 spring.datasource.max-wait=1000 spring.datasource.min-idle=5 spring.datasource.initial-size=5 server.port=8012 server.session.timeout=10 server.tomcat.uri-encoding=UTF-8
新建Controller类测试数据库连接,实例如下:
package com.example.demo; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/mydb") public class DBController { @Autowired private JdbcTemplate jdbcTemplate; @RequestMapping("/getUsers") public List
运行App输入地址输出数据库数据。
2. 集成Mybatis
添加mybatis依赖,在pom.xml文件中增加如下:
org.mybatis.spring.boot mybatis-spring-boot-starter 1.0.0
在resource文件夹下添加application.properties配置文件并输入数据库参数,如下:
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.max-idle=10 spring.datasource.max-wait=1000 spring.datasource.min-idle=5 spring.datasource.initial-size=5 server.port=8012 server.session.timeout=10 server.tomcat.uri-encoding=UTF-8
依次添加mapper的接口类和xml文件,类分别如下:
AppMessageMapper.java
package com.example.demo.mapper; import java.util.List; import com.example.demo.bean.AppMessage; public interface AppMessageMapper { int deleteByPrimaryKey(String id); int insert(AppMessage record); int insertSelective(AppMessage record); AppMessage selectByPrimaryKey(String id); int updateByPrimaryKeySelective(AppMessage record); int updateByPrimaryKey(AppMessage record); ListselectAll(); List getMessById(String id); }
AppMessageMapper.xml
id, message, senddate delete from appuser_message where id = #{id,jdbcType=VARCHAR} insert into appuser_message (id, message, senddate ) values (#{id,jdbcType=VARCHAR}, #{message,jdbcType=VARCHAR}, #{senddate,jdbcType=TIMESTAMP} ) insert into appuser_message id, message, senddate, #{id,jdbcType=VARCHAR}, #{message,jdbcType=VARCHAR}, #{senddate,jdbcType=TIMESTAMP}, update appuser_message where id = #{id,jdbcType=VARCHAR} message = #{message,jdbcType=VARCHAR}, senddate = #{senddate,jdbcType=TIMESTAMP}, update appuser_message set message = #{message,jdbcType=VARCHAR}, senddate = #{senddate,jdbcType=TIMESTAMP} where id = #{id,jdbcType=VARCHAR}
AppMessage.java
package com.example.demo.bean; import java.util.Date; public class AppMessage { private String id; private String message; private Date senddate; public String getId() { return id; } public void setId(String id) { this.id = id == null ? null : id.trim(); } public String getMessage() { return message; } public void setMessage(String message) { this.message = message == null ? null : message.trim(); } public Date getSenddate() { return senddate; } public void setSenddate(Date senddate) { this.senddate = senddate; } }
AppMessageService.java
package com.example.demo.service; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.example.demo.bean.AppMessage; import com.example.demo.mapper.AppMessageMapper; @Service public class AppMessageService { @Autowired private AppMessageMapper mapper; public ListgetMessage(){ List list = new ArrayList (); list.add(mapper.selectByPrimaryKey("xtt")); //list = mapper.selectAll(); return list; } public List getAllMessage(){ List list = new ArrayList (); list = mapper.selectAll(); return list; } public int addMessage(AppMessage appMessage) { return mapper.insert(appMessage); } public List getMessageById(String id) { return mapper.getMessById(id); } public int delMessage(String id) { return mapper.deleteByPrimaryKey(id); } }
APPMessageController.java
package com.example.demo.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.example.demo.bean.AppMessage; import com.example.demo.service.AppMessageService; @RestController @RequestMapping("/appmessage") public class APPMessageController { @Autowired private AppMessageService service; @RequestMapping("/getThree") public ListgetThreeForMessage(){ List list = service.getMessage(); return list; } @RequestMapping("/getAll") public List getAllMessage(){ List list = service.getAllMessage(); int num = list.size(); if(null!=list && num>3){ for (int i = 0; i < num-3; i++) { list.remove(0); } } return list; } @RequestMapping("/getByID") public List getMessageById(@RequestParam("id") String id){ List list = service.getMessageById(id); int num = list.size(); if(null!=list && num>5){ for (int i = 0; i < num-5; i++) { list.remove(0); } } return list; } @RequestMapping(value = "/add",method = RequestMethod.POST) // 或者采用@PostMapping("/add")方法,更加节省代码的编写量 public int addMessage(@RequestBody AppMessage appMessage){ return service.addMessage(appMessage); } @RequestMapping(value="/delMessageById",method=RequestMethod.POST) // 或者采用@PostMapping("/delMessageById")方法,更加节省代码的编写量 public int delMessageById(@RequestParam("id") String id){ return service.delMessage(id); } }
问题描述?
SpringBoot扫描包提示找不到mapper的问题,异常信息:
Consider defining a bean of type in your configuration
分析原因
Spring Boot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描,“Application类”是指Spring Boot项目入口类。如果Application类所在的包为:com.yoodb.blog,则只会扫描com.yoodb.blog包及其所有子包,如果service或dao所在包不在com.yoodb.blog及其子包下,则不会被扫描。
解决方法
方式一:使用注解@ComponentScan(value=”com.yoodb.blog”),其中,com.yoodb.blog为包路径。
方式二:将启动类Application放在上一级包中,注意的是Application启动类必须要保证在包的根目录下。
关于“Spring Boo如何t连接MySql数据库”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
新闻名称:SpringBoo如何t连接MySql数据库
网址分享:http://scyingshan.cn/article/ghohjh.html