springboot使用入门
2021-01-20 05:14
标签:mapping dbcp nsa nal min connect and start pre 1.创建maven工程,引入依赖 2.引入SpringBoot提供的自动配置依赖 3.添加全局引导类,并使用@SpringBootApplication注解 4.引入druid依赖 5.添加jdbc.properties配置文件 6使用 @Autowired注入datasource 7.添加连接池 只需要在pom中找到springboot提供的启动器 8.指定连接池参数 在resource目录下创建:application.properties配置文件 9.使用druid连接池 在application.properties中添加一些连接属性 10.整合mybatis 10.1配置 mybatis别名扫描 10.2编写dao类 11.通用mapper 引入启动类 12.编写server类 注意添加事务 13编写controller类 application.properties配置文件 pom.xml配置文件 结束! springboot使用入门 标签:mapping dbcp nsa nal min connect and start pre 原文地址:https://www.cnblogs.com/zxh06820/p/12903122.html
@SpringBootApplication // 相当于 EnableAutoConfiguration + ComponentScan + SpringbootConfiguration
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
}
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/springboottest
jdbc.username=root
jdbc.password=root
@Configuration
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfiguration {
@Autowired
private JdbcProperties jdbcProperties;
@Bean
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(jdbcProperties.getDriverClassName());
dataSource.setUrl(jdbcProperties.getUrl());
dataSource.setUsername(jdbcProperties.getUsername());
dataSource.setPassword(jdbcProperties.getPassword());
return dataSource;
}
}
# 连接四大参数
spring.datasource.url=jdbc:mysql://localhost:3306/springboottest
spring.datasource.username=root
spring.datasource.password=root
# 可省略,SpringBoot自动推断
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#初始化连接数
spring.datasource.druid.initial-size=1
#最小空闲连接
spring.datasource.druid.min-idle=1
#最大活动连接
spring.datasource.druid.max-active=20
#获取连接时测试是否可用
spring.datasource.druid.test-on-borrow=true
#监控页面启动
spring.datasource.druid.stat-view-servlet.allow=true
# mybatis 别名扫描
mybatis.type-aliases-package=cn.test.pojo
@Mapper
public interface UserMapper {
}
@Transactional
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User queryUserById(Long id){
return this.userMapper.selectByPrimaryKey(id);
}
@Transactional
public void deleteUserById(Long id){
this.userMapper.deleteByPrimaryKey(id);
}
}
@RestController
@RequestMapping("User")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("{id}")
@ResponseBody
public User queryUserById(@PathVariable("id")Long id){
return this.userService.queryUserById(id);
}
}
server.port=8888
spring.mvc.static-path-pattern=/**
# 这个配置是为了能访问resource/static/目录下的静态资源
# 连接四大参数
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
# 可省略,SpringBoot自动推断
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#初始化连接数
spring.datasource.druid.initial-size=1
#最小空闲连接
spring.datasource.druid.min-idle=1
#最大活动连接
spring.datasource.druid.max-active=20
#获取连接时测试是否可用
spring.datasource.druid.test-on-borrow=true
#监控页面启动
spring.datasource.druid.stat-view-servlet.allow=true
# mybatis 别名扫描
mybatis.type-aliases-package=com.test.pojo