yang嗯嗯要来充实自己了。
OK,那就从Springboot开始吧。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
@SpringBootApplication
public class BootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(BootDemoApplication.class, args);
}
}
呐,环境已搭好。
@Controller
public class HelloController {
@GetMapping("hello")
@ResponseBody
public String hello(){
return "hello, spring boot";
}
}
@RestController注解相当于@ResponseBody + @Controller合在一起的作用。
现在来看连接池的java配置喽!
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
再建一个jdbc.properties文件
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/eesy
jdbc.username=root
jdbc.password=12345678
@Configuration
@PropertySource("classpath:jdbc.properties")
public class JdbcConfig {
@Value("${jdbc.url}")
String url;
@Value("${jdbc.driverClassName}")
String driverClassName;
@Value("${jdbc.username}")
String username;
@Value("${jdbc.password}")
String password;
@Bean
public DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
springboot属性注入
方式一
改名字:application.properties
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
@ConfigurationProperties(prefix = "jdbc")
@Data
public class JdbcProperties {
String url;
String driverClassName;
String username;
String password;
}
@Configuration
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfig {
@Bean
public DataSource dataSource(JdbcProperties prop){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(prop.getDriverClassName());
dataSource.setUrl(prop.getUrl());
dataSource.setUsername(prop.getUsername());
dataSource.setPassword(prop.getPassword());
return dataSource;
}
}
方式二
@Data
public class JdbcProperties {
String url;
String driverClassName;
String username;
String password;
}
@Configuration
public class JdbcConfig {
@Bean
@ConfigurationProperties(prefix = "jdbc")
public DataSource dataSource(){
return new DruidDataSource();
}
}
属性配置文件
yaml文件
jdbc:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/eesy
username: root
password: 12345678
user:
name: jack
age: 21
language:
- java
- php
- ios
自动配置原理
yaml文件和properties两个文件都有取并集,若重名properties文件优先。
整合连接池
<!--连接池-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
然后在application.yaml文件中配置数据库连接的四大参数
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/eesy
username: root
password: 12345678
整合mybatis
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
开启自动驼峰匹配
mybatis:
configuration:
map-underscore-to-camel-case: true
配置mapper文件所在位置(一般不用该配置,大多数情况下用的通用mapper)
mybatis:
configuration:
mapper-locations: mapper/*.xml
配置别名扫描包
mybatis:
type-aliases-package: enen.pojo
mapper扫描器(接口):通过添加@MapperScan注解
@SpringBootApplication
@MapperScan("enen.mapper")
public class BootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(BootDemoApplication.class, args);
}
}
附加通用mapper
https://github.com/abel533/Mapper
<!--通用mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.3</version>
</dependency>
import tk.mybatis.mapper.common.Mapper;
public interface UserMapper extends Mapper<User> {
}
一旦引入通用mapper,mybatis和jdbc连接池的依赖不需要,也会开启自动驼峰匹配。另外不能使用import org.mybatis.spring.annotation.MapperScan下的@MapperScan了,应该使用import tk.mybatis.spring.annotation.MapperScan;
当数据库表名和实体名不一致时,在实体类上加入@Table注解,在主键加@Id,主键是自增长的加@KeySql,其他字段若不是都作为sql字段来用加@Transient
@Data
@Table(name="tb_user")
public class User {
@Id
@KeySql(useGeneratedKeys = true)
private int id;
private String username;
private String password;
private String name;
private int age;
private int sex;
private Date birthday;
@Transient
private String note;
}
OK,接下来测试一下通用mapper
先引入test启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void testQuery(){
User user = userMapper.selectByPrimaryKey(2);
System.out.println("user=" + user);
}
}
写service
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User queryById(int id){
return userMapper.selectByPrimaryKey(id);
}
public void insertUser(User user){
userMapper.insert(user);
}
}
写controller
@Slf4j
@Controller
@RequestMapping("user")
public class HelloController {
@Autowired
private UserService userService;
@GetMapping("{id}")
@ResponseBody
public User hello(@PathVariable("id") int id){
return userService.queryById(id);
}
}
ok,撒花🎉完结。