刚接触springboot,初次整合,简单的数据连接,分享一下自己的配置。
1.目录结构:
2.参数配置application.yml
#jsp文件路径,默认路径:src/main/webapp,
spring:
#redis配置
redis:
host: localhost
port:6379password:123456# 连接超时时间 单位 ms(毫秒)
timeout:3000max-idle: 200max-active: 2000max-wait: 1000#mysql配置
datasource:
url: jdbc:mysql://localhost:3306/test_data
username: root
password:123456driver-class-name: com.mysql.jdbc.Driver
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
max-idle: 10max-wait: 1000min-idle: 5initial-size: 5dbcp2:
validation-query: SELRCT 1test-on-borrow: truemybatis:
mapperLocations: classpath:mappers/*.xml
#domain object's package
typeAliasesPackage: com.zykj.projectforspringboot.bean
#配置扫描Mapper
#mapper:
# #多个mapper用逗号隔开,(注解扫描@MapperScan("com.zykj.projectforspringboot.mappers.GirlMapper) :标识持久层mapper接口,用来找到mapper对象)
# mappers: com.zykj.projectforspringboot.mappers
# not-empty: false
# identity: MYSQL
#配置
mvc:
view:
prefix: /
suffix: .jsp
http:
encoding:
charset: UTF-8
enabled: true
force: true
server:
port: 8080
session:
timeout: 10
tomcat:
uri-encoding: UTF-8
2.pom文件
4.0.0
com.zykj
projectforspringboot
0.0.1-SNAPSHOT
jar
projectforspringboot
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.3.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-test
2.0.1.RELEASE
org.springframework.boot
spring-boot-starter-cache
2.0.3.RELEASE
junit
junit
4.12
org.springframework.boot
spring-boot-starter-data-redis
1.5.9.RELEASE
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1
mysql
mysql-connector-java
5.1.46
com.alibaba
druid
1.1.0
org.springframework.boot
spring-boot-maven-plugin
3.mapper映射文件
mapper.xml在yml中配置扫描mapperLocations: classpath:mappers/*.xml
insert into test_girl ( name, age, cup_size)
values (#{name,jdbcType=VARCHAR}, #{age,jdbcType=BIGINT},
#{cupSize,jdbcType=VARCHAR})
SELECT* FROM test_girl WHERE id = #{id,jdbcType=BIGINT}
GirlMapper接口使用@Mapper注解或在加载配置文件时使用包扫描@MapperScan(basePackages = "com.zykj.projectforspringboot.mappers")多个mappper包用逗号隔开。
package com.zykj.projectforspringboot.mappers;
import org.apache.ibatis.annotations.Mapper;
import com.zykj.projectforspringboot.bean.Girl;//@Mapper
public interfaceGirlMapper {public voidinsertGirl(Girl girl);public Girl selectGirl(intid);
}
4.测试类,
首先,提出一个基础的测试类BaseTest.java
package com.zykj.projectforspringboot.commons;
import com.zykj.projectforspringboot.ProjectforspringbootApplication;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=ProjectforspringbootApplication.class)
@WebAppConfiguration
@MapperScan(value= "com.zykj.projectforspringboot.mappers")public classBaseTest {
}
TestMysql.java和 TestRedis.java
package com.zykj.projectforspringboot.testMysql;
import com.zykj.projectforspringboot.bean.Girl;
import com.zykj.projectforspringboot.commons.BaseTest;
import com.zykj.projectforspringboot.mappers.GirlMapper;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.TestExecutionListeners;
import javax.swing.plaf.PanelUI;public classTestMysql extends BaseTest {
@AutowiredprivateGirlMapper girlMapper;
@Testpublic voidinsert(){
Girl girl= newGirl();
girl.setAge(15);
girl.setCupSize("C");
girl.setName("Lily");
girlMapper.insertGirl(girl);
System.out.println("保存成功!");
}
@Testpublic voidgetGirl(){
Girl girl= girlMapper.selectGirl(1);
System.out.println("girl`s age =" +girl.getAge());
}
}
package com.zykj.projectforspringboot.testRedis;
import com.zykj.projectforspringboot.commons.BaseTest;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;public classtestRedis extends BaseTest {
@AutowiredprivateStringRedisTemplate stringRedisTemplate;
@Testpublic voidgetValue(){
String age= null;try{
stringRedisTemplate.opsForValue().set("Jack","23");
age= stringRedisTemplate.opsForValue().get("Jack");
Boolean isDelete= stringRedisTemplate.delete("Jack");
System.out.println("删除成功!");
}catch(Exception e) {
e.printStackTrace();
}
System.out.println("Jack 的年龄:"+age);
}
}
5.实体类
在yml文件中通过typeAliasesPackage: com.zykj.projectforspringboot.bean扫描实体类
package com.zykj.projectforspringboot.bean;
import java.io.Serializable;public classGirl implements Serializable {private static final long serialVersionUID = -1658170337012912131L;private intid;privateString name;private intage;privateString cupSize;public intgetId() {returnid;
}public void setId(intid) {this.id =id;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}public intgetAge() {returnage;
}public void setAge(intage) {this.age =age;
}publicString getCupSize() {returncupSize;
}public voidsetCupSize(String cupSize) {this.cupSize =cupSize;
}
}
在测试过程中pom文件引用jar包的版本过低会引起一些错误,数据库连接(例如mysql-connector-java包之前用5.1.21版本一直报错java.math.BigInteger cannot be cast to java.lang.Long),(例如mybatis-spring-boot-starter之前使用1.0,0一直引不到@Mapper注解),这都是之前踩过的坑,记录下来为以后自己和他人搭建环境是少走弯路。