<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://192.168.15.22:3306/jdbc
driver-class-name: com.mysql.jdbc.Driver
效果:
? 默認是用org.apache.tomcat.jdbc.pool.DataSource作為數(shù)據(jù)源;
? 數(shù)據(jù)源的相關(guān)配置都在DataSourceProperties里面;
自動配置原理:
org.springframework.boot.autoconfigure.jdbc:
1算灸、參考DataSourceConfiguration壁却,根據(jù)配置創(chuàng)建數(shù)據(jù)源语卤,默認使用Tomcat連接池眶根;可以使用spring.datasource.type指定自定義的數(shù)據(jù)源類型豺谈;
2比藻、SpringBoot默認可以支持铝量;
org.apache.tomcat.jdbc.pool.DataSource、HikariDataSource银亲、BasicDataSource慢叨、
3、自定義數(shù)據(jù)源類型
/**
* Generic DataSource configuration.
*/
@ConditionalOnMissingBean(DataSource.class)
@ConditionalOnProperty(name = "spring.datasource.type")
static class Generic {
@Bean
public DataSource dataSource(DataSourceProperties properties) {
//使用DataSourceBuilder創(chuàng)建數(shù)據(jù)源务蝠,利用反射創(chuàng)建響應(yīng)type的數(shù)據(jù)源拍谐,并且綁定相關(guān)屬性
return properties.initializeDataSourceBuilder().build();
}
}
4、DataSourceInitializer:ApplicationListener;
? 作用:
? 1)轩拨、runSchemaScripts();運行建表語句践瓷;
? 2)、runDataScripts();運行插入數(shù)據(jù)的sql語句亡蓉;
默認只需要將文件命名為:
schema-*.sql搭综、data-*.sql
默認規(guī)則:schema.sql区丑,schema-all.sql嘱蛋;
可以使用
schema:
- classpath:department.sql
指定位置
5叫潦、操作數(shù)據(jù)庫:自動配置了JdbcTemplate操作數(shù)據(jù)庫
2、整合Druid數(shù)據(jù)源
導(dǎo)入druid數(shù)據(jù)源
@Configuration
public class DruidConfig {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druid(){
return new DruidDataSource();
}
//配置Druid的監(jiān)控
//1爸邢、配置一個管理后臺的Servlet
@Bean
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
Map<String,String> initParams = new HashMap<>();
initParams.put("loginUsername","admin");
initParams.put("loginPassword","123456");
initParams.put("allow","");//默認就是允許所有訪問
initParams.put("deny","192.168.15.21");
bean.setInitParameters(initParams);
return bean;
}
//2巫员、配置一個web監(jiān)控的filter
@Bean
public FilterRegistrationBean webStatFilter(){
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
Map<String,String> initParams = new HashMap<>();
initParams.put("exclusions","*.js,*.css,/druid/*");
bean.setInitParameters(initParams);
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}
}
3、整合MyBatis
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
步驟:
? 1)甲棍、配置數(shù)據(jù)源相關(guān)屬性(見上一節(jié)Druid)
? 2)、給數(shù)據(jù)庫建表
? 3)赶掖、創(chuàng)建JavaBean
4)感猛、注解版
//指定這是一個操作數(shù)據(jù)庫的mapper
@Mapper
public interface DepartmentMapper {
@Select("select * from department where id=#{id}")
public Department getDeptById(Integer id);
@Delete("delete from department where id=#{id}")
public int deleteDeptById(Integer id);
@Options(useGeneratedKeys = true,keyProperty = "id")
@Insert("insert into department(departmentName) values(#{departmentName})")
public int insertDept(Department department);
@Update("update department set departmentName=#{departmentName} where id=#{id}")
public int updateDept(Department department);
}
問題:
自定義MyBatis的配置規(guī)則;給容器中添加一個ConfigurationCustomizer奢赂;
@org.springframework.context.annotation.Configuration
public class MyBatisConfig {
@Bean
public ConfigurationCustomizer configurationCustomizer(){
return new ConfigurationCustomizer(){
@Override
public void customize(Configuration configuration) {
configuration.setMapUnderscoreToCamelCase(true);
}
};
}
}
使用MapperScan批量掃描所有的Mapper接口陪白;
@MapperScan(value = "com.atguigu.springboot.mapper")
@SpringBootApplication
public class SpringBoot06DataMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBoot06DataMybatisApplication.class, args);
}
}
5)、配置文件版
mybatis:
config-location: classpath:mybatis/mybatis-config.xml 指定全局配置文件的位置
mapper-locations: classpath:mybatis/mapper/*.xml 指定sql映射文件的位置
更多使用參照
http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
2)膳灶、整合SpringData JPA
JPA:ORM(Object Relational Mapping)咱士;
1)、編寫一個實體類(bean)和數(shù)據(jù)表進行映射轧钓,并且配置好映射關(guān)系序厉;
//使用JPA注解配置映射關(guān)系
@Entity //告訴JPA這是一個實體類(和數(shù)據(jù)表映射的類)
@Table(name = "tbl_user") //@Table來指定和哪個數(shù)據(jù)表對應(yīng);如果省略默認表名就是user;
public class User {
@Id //這是一個主鍵
@GeneratedValue(strategy = GenerationType.IDENTITY)//自增主鍵
private Integer id;
@Column(name = "last_name",length = 50) //這是和數(shù)據(jù)表對應(yīng)的一個列
private String lastName;
@Column //省略默認列名就是屬性名
private String email;
2)毕箍、編寫一個Dao接口來操作實體類對應(yīng)的數(shù)據(jù)表(Repository)
//繼承JpaRepository來完成對數(shù)據(jù)庫的操作
public interface UserRepository extends JpaRepository<User,Integer> {
}
3)弛房、基本的配置JpaProperties
spring:
jpa:
hibernate:
# 更新或者創(chuàng)建數(shù)據(jù)表結(jié)構(gòu)
ddl-auto: update
# 控制臺顯示SQL
show-sql: true