Spring Boot高级教程之Spring Boot连接MySql数据库
SpringBoot可以大大简化持久化任务,几乎不需要写SQL语句,在之前章节“SpringBoot构建框架”中我们新建了一个SpringBoot应用程序,本章在原有的工程中与数据库建立连接。
SpringBoot有两种方法与数据库建立连接,一种是使用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类测试数据库连接,实例如下:
packagecom.example.demo;
importjava.util.Iterator;
importjava.util.List;
importjava.util.Map;
importjava.util.Map.Entry;
importjava.util.Set;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.jdbc.core.JdbcTemplate;
importorg.springframework.web.bind.annotation.PathVariable;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/mydb")
publicclassDBController{
@Autowired
privateJdbcTemplatejdbcTemplate;
@RequestMapping("/getUsers")
publicList
运行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
packagecom.example.demo.mapper;
importjava.util.List;
importcom.example.demo.bean.AppMessage;
publicinterfaceAppMessageMapper{
intdeleteByPrimaryKey(Stringid);
intinsert(AppMessagerecord);
intinsertSelective(AppMessagerecord);
AppMessageselectByPrimaryKey(Stringid);
intupdateByPrimaryKeySelective(AppMessagerecord);
intupdateByPrimaryKey(AppMessagerecord);
ListselectAll();
ListgetMessById(Stringid);
}
AppMessageMapper.xml
id,message,senddate select fromappuser_message whereid=#{id,jdbcType=VARCHAR} deletefromappuser_message whereid=#{id,jdbcType=VARCHAR} insertintoappuser_message(id,message,senddate ) values(#{id,jdbcType=VARCHAR},#{message,jdbcType=VARCHAR},#{senddate,jdbcType=TIMESTAMP} ) insertintoappuser_message id, message, senddate, #{id,jdbcType=VARCHAR}, #{message,jdbcType=VARCHAR}, #{senddate,jdbcType=TIMESTAMP}, updateappuser_message whereid=#{id,jdbcType=VARCHAR} message=#{message,jdbcType=VARCHAR}, senddate=#{senddate,jdbcType=TIMESTAMP}, updateappuser_message setmessage=#{message,jdbcType=VARCHAR}, senddate=#{senddate,jdbcType=TIMESTAMP} whereid=#{id,jdbcType=VARCHAR} select id,message,senddate fromappuser_message orderbysenddateasc select id,message,senddate from appuser_message whereid=#{id,jdbcType=VARCHAR} orderbysenddateasc
AppMessage.java
packagecom.example.demo.bean;
importjava.util.Date;
publicclassAppMessage{
privateStringid;
privateStringmessage;
privateDatesenddate;
publicStringgetId(){
returnid;
}
publicvoidsetId(Stringid){
this.id=id==null?null:id.trim();
}
publicStringgetMessage(){
returnmessage;
}
publicvoidsetMessage(Stringmessage){
this.message=message==null?null:message.trim();
}
publicDategetSenddate(){
returnsenddate;
}
publicvoidsetSenddate(Datesenddate){
this.senddate=senddate;
}
}
AppMessageService.java
packagecom.example.demo.service;
importjava.util.ArrayList;
importjava.util.List;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;
importcom.example.demo.bean.AppMessage;
importcom.example.demo.mapper.AppMessageMapper;
@Service
publicclassAppMessageService{
@Autowired
privateAppMessageMappermapper;
publicListgetMessage(){
Listlist=newArrayList();
list.add(mapper.selectByPrimaryKey("xtt"));
//list=mapper.selectAll();
returnlist;
}
publicListgetAllMessage(){
Listlist=newArrayList();
list=mapper.selectAll();
returnlist;
}
publicintaddMessage(AppMessageappMessage){
returnmapper.insert(appMessage);
}
publicListgetMessageById(Stringid){
returnmapper.getMessById(id);
}
publicintdelMessage(Stringid){
returnmapper.deleteByPrimaryKey(id);
}
}
APPMessageController.java
packagecom.example.demo.controller;
importjava.util.List;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.web.bind.annotation.RequestBody;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestMethod;
importorg.springframework.web.bind.annotation.RequestParam;
importorg.springframework.web.bind.annotation.RestController;
importcom.example.demo.bean.AppMessage;
importcom.example.demo.service.AppMessageService;
@RestController
@RequestMapping("/appmessage")
publicclassAPPMessageController{
@Autowired
privateAppMessageServiceservice;
@RequestMapping("/getThree")
publicListgetThreeForMessage(){
Listlist=service.getMessage();
returnlist;
}
@RequestMapping("/getAll")
publicListgetAllMessage(){
Listlist=service.getAllMessage();
intnum=list.size();
if(null!=list&&num>3){
for(inti=0;igetMessageById(@RequestParam("id")Stringid){
Listlist=service.getMessageById(id);
intnum=list.size();
if(null!=list&&num>5){
for(inti=0;i
问题描述?
SpringBoot扫描包提示找不到mapper的问题,异常信息:
Considerdefiningabeanoftypeinyourconfiguration
分析原因
SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描,“Application类”是指SpringBoot项目入口类。如果Application类所在的包为:com.yoodb.blog,则只会扫描com.yoodb.blog包及其所有子包,如果service或dao所在包不在com.yoodb.blog及其子包下,则不会被扫描。
解决方法
方式一:使用注解@ComponentScan(value=”com.yoodb.blog”),其中,com.yoodb.blog为包路径。
方式二:将启动类Application放在上一级包中,注意的是Application启动类必须要保证在包的根目录下。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。