1.创建springboot 3个关键类
1.1 属性读取类:用于从配置文件中读取配置信息
package com.shijt.springbootdemo.jdbc; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "mysql.jdbc") public class JDBCRead { private String driver; private String url; private String username; private String password; public String getDriver() { return driver; } public void setDriver(String driver) { this.driver = driver; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
1.2 核心事件类
package com.shijt.springbootdemo.jdbc; import java.sql.*; public class JDBCUtils { private String driver; private String url; private String username; private String password; public int testJdbc(){ Connection con = null; try{ //加载MySql的驱动类 Class.forName("com.mysql.jdbc.Driver") ; con = DriverManager.getConnection(url , username , password ) ; //仅做测试,直接写死了sql String sql="INSERT INTO `smbms_user` (`id`, `userCode`, `userName`) VALUES (‘001‘, ‘001‘, ‘zhangsan‘)"; PreparedStatement pstmt = con.prepareStatement(sql) ; int rows = pstmt.executeUpdate() ; pstmt.close(); return rows; }catch(ClassNotFoundException e){ System.out.println("找不到驱动程序类 ,加载驱动失败!"); e.printStackTrace() ; }catch(SQLException se){ System.out.println("数据库连接失败!"); se.printStackTrace() ; }catch (Exception e){ e.printStackTrace(); }finally { if (con != null){ try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } return 0; } public String getDriver() { return driver; } public void setDriver(String driver) { this.driver = driver; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
1.3 整合类:把属性读取类中的属性赋值给核心事件类
package com.shijt.springbootdemo.jdbc; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.annotation.Resource; @Configuration @EnableConfigurationProperties({JDBCRead.class}) @ConditionalOnClass({JDBCUtils.class }) @ConditionalOnProperty(prefix="mysql.jdbc",value = "enabled",matchIfMissing = true) public class JDBCAutoConfigration { @Resource private JDBCRead jdbcRead; @Bean @ConditionalOnMissingBean({JDBCUtils.class}) public JDBCUtils getJDBCUtils(){ //确定方法执行的时间(在springboot启动时,而不是调用JDBCUtils时) System.out.println("-----step into JDBCAutoConfigration getJDBCUtils-----"); JDBCUtils jdbcUtils=new JDBCUtils(); jdbcUtils.setDriver(jdbcRead.getDriver()); jdbcUtils.setUrl(jdbcRead.getUrl()); jdbcUtils.setUsername(jdbcRead.getUsername()); jdbcUtils.setPassword(jdbcRead.getPassword()); return jdbcUtils; } }
2.在resources目录下META-INF文件夹,创建spring.factories文件
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.shijt.springbootdemo.jdbc.JDBCAutoConfigration
把写好的整合类加入到自动配置中
3.编写Controller类,调用JDBCUtils
package com.shijt.springbootdemo.controller; import com.shijt.springbootdemo.jdbc.JDBCUtils; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; @Controller public class UserController { @Resource private JDBCUtils jdbcUtils; @RequestMapping("/testJdbc") @ResponseBody public int testJdbc(){ return jdbcUtils.testJdbc(); } }
在appliaction.yml中配置数据库连接信息
mysql: jdbc: driver: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=utf-8 username: root password: 123456
4.启动springboot,测试
原文地址:https://www.cnblogs.com/shijt/p/11725627.html
时间: 2024-11-05 21:42:25