Spring Boot 以后也许会成为入门Spring的首选!
记一下Spring Boot 成功连接Mysql数据库的方法步骤!
一、新建Maven工程,不全Maven所需文件夹,在pom.xml引入SpringBoot的依赖包!可以参照:http://www.cnblogs.com/liangblog/p/5207855.html
二、有两种方法与数据库建立连接,一种是集成Mybatis,另一种用JdbcTemplate
(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=123456spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10spring.datasource.max-wait=10000spring.datasource.min-idle=5spring.datasource.initial-size=5server.port=8011server.session.timeout=10server.tomcat.uri-encoding=UTF-8
application.properties
新建Controller类测试数据库连接
packagecom.lgp.SpringBoot;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")public classDbController {
@AutowiredprivateJdbcTemplate jdbcTemplate;
@RequestMapping("/getUsers")public List>getDbType(){
String sql= "select * from appuser";
List> list =jdbcTemplate.queryForList(sql);for (Mapmap : list) {
Set> entries =map.entrySet( );if(entries != null) {
Iterator> iterator =entries.iterator( );while(iterator.hasNext( )) {
Entry entry =(Entry) iterator.next( );
Object key=entry.getKey( );
Object value=entry.getValue();
System.out.println(key+":"+value);
}
}
}returnlist;
}
@RequestMapping("/user/{id}")public MapgetUser(@PathVariable String id){
Map map = null;
List> list =getDbType();for (Mapdbmap : list) {
Set set =dbmap.keySet();for(String key : set) {if(key.equals("id")){if(dbmap.get(key).equals(id)){
map=dbmap;
}
}
}
}if(map==null)
map= list.get(0);returnmap;
}
}
DbController.java
运行App 输入地址 输出数据库数据。。。。。。
2)、集成Mybatis
添加mybatis依赖
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.0.0
版本号可能有更新!
在配置文件中添加配置信息:
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test
spring.datasource.username=root
spring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10spring.datasource.max-wait=10000spring.datasource.min-idle=5spring.datasource.initial-size=5server.port=8011server.session.timeout=10server.tomcat.uri-encoding=UTF-8# mybatis.config= classpath:mybatis-config.xml
mybatis.mapperLocations=classpath:mappers/*.xml
# domain object's package
mybatis.typeAliasesPackage=com.lgp.SpringBoot.bean
# handler's package
# mybatis.typeHandlersPackage=
# check the mybatis configuration exists
# mybatis.check-config-location=
# mode of execution. Default is SIMPLE
# mybatis.executorType=
依次添加mapper的接口类和xml文件
主要代码:
packagecom.lgp.SpringBoot.mapper;importjava.util.List;importcom.lgp.SpringBoot.bean.AppMessage;public interfaceAppMessageMapper {intdeleteByPrimaryKey(String id);intinsert(AppMessage record);intinsertSelective(AppMessage record);
AppMessage selectByPrimaryKey(String id);intupdateByPrimaryKeySelective(AppMessage record);intupdateByPrimaryKey(AppMessage record);
ListselectAll();
ListgetMessById(String id);
}
AppMessageMapper.java
id, message, senddate
selectfrom appuser_message
where id = #{id,jdbcType=VARCHAR}
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
message = #{message,jdbcType=VARCHAR},
senddate = #{senddate,jdbcType=TIMESTAMP},
where id = #{id,jdbcType=VARCHAR}
update appuser_message
set message = #{message,jdbcType=VARCHAR},
senddate = #{senddate,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=VARCHAR}
select
id, message, senddate
from appuser_message
order by senddate asc
select
id, message, senddate
from
appuser_message
where id = #{id,jdbcType=VARCHAR}
order by senddate asc
AppMessageMapper.xml
packagecom.lgp.SpringBoot.bean;importjava.util.Date;public classAppMessage {privateString id;privateString message;privateDate senddate;publicString getId() {returnid;
}public voidsetId(String id) {this.id = id == null ? null: id.trim();
}publicString getMessage() {returnmessage;
}public voidsetMessage(String message) {this.message = message == null ? null: message.trim();
}publicDate getSenddate() {returnsenddate;
}public voidsetSenddate(Date senddate) {this.senddate =senddate;
}
}
AppMessage.java
packagecom.lgp.SpringBoot.service;importjava.util.ArrayList;importjava.util.List;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importcom.lgp.SpringBoot.bean.AppMessage;importcom.lgp.SpringBoot.mapper.AppMessageMapper;
@Servicepublic classAppMessageService {
@AutowiredprivateAppMessageMapper mapper;public ListgetMessage(){
List list = new ArrayList();
list.add(mapper.selectByPrimaryKey("xtt"));//list = mapper.selectAll();
returnlist;
}public ListgetAllMessage(){
List list = new ArrayList();
list=mapper.selectAll();returnlist;
}public intaddMessage(AppMessage appMessage) {returnmapper.insert(appMessage);
}public ListgetMessageById(String id) {returnmapper.getMessById(id);
}public intdelMessage(String id) {returnmapper.deleteByPrimaryKey(id);
}
}
AppMessageService.java
packagecom.lgp.SpringBoot.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.lgp.SpringBoot.bean.AppMessage;importcom.lgp.SpringBoot.service.AppMessageService;
@RestController
@RequestMapping("/appmessage")public classAPPMessageController {
@AutowiredprivateAppMessageService service;
@RequestMapping("/getThree")public ListgetThreeForMessage(){
List list =service.getMessage();returnlist;
}
@RequestMapping("/getAll")public ListgetAllMessage(){
List list =service.getAllMessage();int num =list.size();if(null!=list && num>3){for (int i = 0; i < num-3; i++) {
list.remove(0);
}
}returnlist;
}
@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);
}
}returnlist;
}
@RequestMapping(value= "/add",method =RequestMethod.POST)public intaddMessage(@RequestBody AppMessage appMessage){returnservice.addMessage(appMessage);
}
@RequestMapping(value="/delMessageById",method=RequestMethod.POST)public int delMessageById(@RequestParam("id") String id){returnservice.delMessage(id);
}
}
APPMessageController.java
运行App 输入地址测试,获取数据库数据......