Dependent
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
Application-database.properties
#初始化数据库的时候,如果错误,是否继续启动。spring.datasource.continue-on-error=false#jdbc driver.默认通过uri来自动检测。spring.datasource.driver-class-name=com.mysql.jdbc.Driver#jdbc url.连接数据库的urispring.datasource.url=jdbc:mysql://172.28.1.227:3310/fc?useUnicode=true&autoReconnect=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useServerPrepStmts=false#数据库连接用户名spring.datasource.username=fcdev#数据连接密码spring.datasource.password=123456#全限定名,连接池。默认自动检测classpathspring.datasource.type=com.zaxxer.hikari.HikariDataSource#sql脚本字符spring.datasource.sql-script-encoding=UTF-8#jdbcTemplate配置参数spring.jdbc.template.fetch-size=-1spring.jdbc.template.max-rows=-1spring.jdbc.template.query-timeout=60
SOURCE-jdbcproperties
@ConfigurationProperties (prefix = "SPRING.JDBC") public class Jdbcproperties {private final Template Templat E = new Template (); Public Template GetTemplate () {return this.template; }/** * {@code jdbctemplate} settings. */public static class Template {/** * number of rows should is fetched from the database when Mor E rows are * needed. Use-1 to use the JDBC driver ' s default configuration. */private int fetchsize =-1; /** * Maximum number of rows. Use-1 to use the JDBC driver ' s default configuration. */private int maxRows =-1; /** * Query timeout. Default is to use the JDBC driver ' s default configuration. If A * Duration suffix is not specified, seconds would be used. */@DurationUnit (chronounit.seconds) private Duration QueryTimeout; }}
Source-jdbctemplateautoconfiguration
@Configuration @conditionalonclass ({datasource.class, jdbctemplate.class}) @ConditionalOnSingleCandidate ( Datasource.class) @AutoConfigureAfter (datasourceautoconfiguration.class) @EnableConfigurationProperties ( Jdbcproperties.class) public class Jdbctemplateautoconfiguration {@Configuration static class Jdbctemplateconfigurat Ion {private Final DataSource DataSource; Private final jdbcproperties properties; Jdbctemplateconfiguration (DataSource DataSource, jdbcproperties properties) {This.datasource = DataSource; This.properties = properties; } @Bean @Primary @ConditionalOnMissingBean (jdbcoperations.class) public JdbcTemplate Jdbctempl Ate () {jdbctemplate jdbctemplate = new JdbcTemplate (This.datasource); Jdbcproperties.template Template = This.properties.getTemplate (); Jdbctemplate.setfetchsize (Template.getfetchsize ()); Jdbctemplate.setmaxrows (Template.getmaxrows ()); if (template.getquerytimeout () = null) {JdbcTemplate. setquerytimeout (int) Templa Te.getquerytimeout (). getseconds ()); } return jdbctemplate; }} @Configuration @Import (jdbctemplateconfiguration.class) Static class Namedparameterjdbctemplateconfigurati On {@Bean @Primary @ConditionalOnSingleCandidate (jdbctemplate.class) @ConditionalOnMissingBea N (namedparameterjdbcoperations.class) public namedparameterjdbctemplate namedparameterjdbctemplate ( JdbcTemplate jdbctemplate) {return new namedparameterjdbctemplate (JdbcTemplate); } }}
JdbcTemplate use
@Repositorypublic class TestJdbcDao { @Autowired private JdbcTemplate jdbcTemplate; public String getMenuNameById(int id){ return jdbcTemplate.queryForObject("select name from t_sys_menu where id = ?", new Object[]{id}, String.class); }}
JdbcTemplate Test
@RunWith(SpringRunner.class)@SpringBootTestpublic class AppContextTest { @Autowired private TestJdbcDao testJdbcDao; @Test public void jdbcTest(){ String menuName = testJdbcDao.getMenuNameById(1); System.out.println("menuName = " + menuName); }
Spring Boot DAO's JdbcTemplate