1.用idea新建spring boot项目
File->New->Project->Spring Initializr (SDK默认选择本机版本)->Next
备注:Spring Initializr 和 maven 的区别:两者都可以创建Maven项目,本质上没多大区别,只在pom依赖的引入时间上有所不同。使用“spring initializr”创建项目,可以直观的手动勾选依赖,项目创建成功后pom.xml中会自带依赖;
使用“Maven”创建项目,需要自己在pom.xml文件夹中添加依赖。
Group:项目的根目录名称;
Artifact:定义当前maven项目在组中的唯一id;
点击Next,选择依赖,也可以不选,直接在pom.xml中需要啥添加啥,点击Next;
最后点击Finish即可。
2.添加相应的包和文件,项目结构如下,其中,圈起来的文件是通过工具生成的,剩余几个文件代码如下。
(1)TestController.java
package com.dxw.demo.controller;
import com.dxw.demo.bean.Demo;
import com.dxw.demo.bean.DemoExample;
import com.dxw.demo.mapper.dxw_demo.DemoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
@RestController
public class TestController {
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
public DemoMapper demoMapper;
//不连接数据库的测试
@GetMapping("/hello")
public String hello() {
return "hello springboot";
}
//连接数据库的测试(无需生成了相应的文件)
@RequestMapping("/test1")
public List<Map<String, Object>> getDbType1() {
String sql = "select * from demo";
List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
for (Map<String, Object> map : list) {
Set<Map.Entry<String, Object>> entries = map.entrySet();
if (entries != null) {
Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
while (iterator.hasNext()) {
Map.Entry<String, Object> entry = (Map.Entry<String, Object>) iterator.next();
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println(key + ":" + value);
}
}
}
return list;
}
//连接数据库的测试(前提生成了相应的文件)
@RequestMapping("/test")
public List<Demo> getDbType() {
DemoExample ex = new DemoExample();
ex.createCriteria();
List<Demo> list = demoMapper.selectByExample(ex);
return list;
}
}
(2)DataSourceConfig.java
package com.dxw.demo.util;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.dxw.demo.mapper.dxw_demo", sqlSessionTemplateRef = "demoSqlSessionTemplate")
public class DataSourceConfig {
@Bean(name = "demoDataSource")
@ConfigurationProperties(prefix = "demo.spring.datasource")
public DataSource setDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "demoTransactionManager")
public DataSourceTransactionManager setTransactionManager(@Qualifier("demoDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "demoSqlSessionFactory")
public SqlSessionFactory setSqlSessionFactory(@Qualifier("demoDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/dxw_demo/*.xml"));
return bean.getObject();
}
@Bean(name = "demoSqlSessionTemplate")
public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("demoSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
(3)DemoApplication .java
package com.dxw.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@PropertySource(value = {"classpath:config/jdbc.properties"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
(4)jdbc.properties(只有4个必填项)
#多个数据源
#spring boot 1.0版本
#demo.spring.datasource.driverClassName = com.mysql.jdbc.Driver
#demo.spring.datasource.url = jdbc:mysql://*/*?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=TRUE
#spring boot 2.0版本
demo.spring.datasource.driver-class-name = com.mysql.jdbc.Driver
demo.spring.datasource.jdbc-url = jdbc:mysql://*/*?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=TRUE
demo.spring.datasource.username = *
demo.spring.datasource.password = *
##验证连接的有效性
#spring.datasource.primary.test-while-idle = true
##获取连接时候验证,会影响性能
#spring.datasource.primary.test-on-borrow = false
##在连接归还到连接池时是否测试该连接
#spring.datasource.primary.test-on-return = false
#spring.datasource.primary.validation-query = SELECT 1 FROM DUAL
##空闲连接回收的时间间隔,与test-while-idle一起使用,设置5分钟
#spring.datasource.primary.time-between-eviction-runs-millis = 300000
##连接池空闲连接的有效时间 ,设置30分钟
#spring.datasource.primary.min-evictable-idle-time-millis = 1800000
#spring.datasource.primary.initial-size = 5
##指定连接池中最大的活跃连接数.
#spring.datasource.primary.max-active=50
##指定连接池等待连接返回的最大等待时间,毫秒单位.
#spring.datasource.primary.max-wait=60000
##指定必须保持连接的最小值
#spring.datasource.primary.min-idle=5
(5)application.properties
空,如果需要修改默认端口,可以在此文件写:server.port=8081
3.启动后结果
(1)不连接数据库:
(2)连接数据库: