仅为记录自己学习过程
前置条件:
假设已有一个MySQL数据库,其中创建一个table叫做people ,有id,name...列.
引入依赖:
在pom文件引入spring-boot-starter-jdbc的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
引入mysql连接类和连接池:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
开启web:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
配置相关文件
在jdbc.properties文件配置mysql的驱动类,数据库地址,数据库账号、密码信息。
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/people
spring.datasource.username=root
spring.datasource.password=root
修改Application.java
/**
* Created by Administrator on 2018/3/1.
*/
@SpringBootApplication
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class,args);
@Autowired
private Environment env;//获取配置文件的一种方式
//destroy-method="close"的作用是当数据库连接不使用的时候,就把该连接重新放到数据池中,方便下次使用调用.
@Bean(destroyMethod = "close")
public DataSource dataSource(){
DruidDataSource dataSource =new DruidDataSource();
dataSource.setUrl(env.getProperty("spring.datasource.url"));
dataSource.setUsername(env.getProperty("spring.datasource.username"));
dataSource.setPassword(env.getProperty("spring.datasource.password"));
dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
dataSource.setInitialSize(2);//初始化时建立物理连接的个数
dataSource.setMaxActive(20);//最大连接池数量
dataSource.setMinIdle(0);//最小连接池数量
dataSource.setMaxWait(60000);//最大等待时间,单位毫秒
dataSource.setValidationQuery("SELECT 1");//用来检测连接是否有效
dataSource.setTestOnBorrow(false);//申请连接时执行validationQuery检测连接是否有效
dataSource.setTestWhileIdle(true);//建议配置为true,不影响性能,并且保证安全性。
dataSource.setPoolPreparedStatements(false);//是否缓存preparedStatement,也就是PSCache
return dataSource;
}
}
根据数据库创建一个实体类
/**
* Created by Administrator on 2018/3/1.
*/
public class People {
private int id ;
private String name ;
...
//省略了setget
}
控制层
@RestController
@RequestMapping("/jdbc")
public class PeopleController {
@Autowired
IJdbcService jdbcService;
@RequestMapping(value = "/list",method = RequestMethod.GET)
public List<People> getPeoples(){
return jdbcService.findPeopleList;
}
}
service层
@Service
public class JdbcServiceImpl implements IJdbcService{
@Autowired
IJdbcDao iJdbcDao;
@Override
public List<People> findPeopleList(){
return iJdbcDao.findPeopleList();
}
public interface IJdbcService {
List<People> findPeopleList();
}
Dao层
public interface IJdbcDao {
List<People> findPeopleList();
}
@Repository
public class JdbcDaoImpl implements IJdbcDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<People> findPeopleList(){
List<People>list=jdbcTemplate.query("select * from table",new Object[]{},new BeanPropertyRowMapper(People.class));
if(list!=null && list.size()>0){
People people =list.get(0);
return people;
}else {
return null;
}
}
参考:
http://blog.csdn.net/forezp/article/details/70477821#reply
http://tengj.top/2017/04/13/springboot8/