目录
一、创建springboot项目
spring boot项目搭建可以通过网页、spring tool suit、intellij idea、spring boot cli等方式。
1、使用网页创建
1、访问http://start.spring.io。
2、按照页面提示选择maven,jdk版本,填写groupid,artifactid等。Search dependencies to add中搜索需要添加的依赖包,比如例如web之类的。
3、点击generate the project,下载压缩包。
4、解压导入idea即可。
2、使用idea spring initializer创建
打开Idea-> new Project ->Spring Initializr ->填写group、artifact等->钩上web(开启web功能)->点下一步就行了。
二、配置文件
当我们创建一个springboot项目的时候,系统默认会为我们在src/main/java/resources目录下创建一个application.properties。
application.properties中自定义配置属性之后,可以在代码中通过@Value(“${属性名}”)引用,注:如果代码中用@Value引用了一个不存在的属性值,项目启动会报错。
多环境配置
在现实的开发环境中,我们需要不同的配置环境;格式为application-{profile}.properties,其中{profile}对应你的环境标识,比如:
application-test.properties:测试环境
application-dev.properties:开发环境
application-prod.properties:生产环境
这时候只需要在application.properties中加入:spring.profiles.active=dev即可将当前环境切换成dev环境。
三、JdbcTemplates访问Mysql
1、在pom文件引入spring-boot-starter-jdbc的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
2、引入mysql连接类和连接池:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
3、开启web
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
4、application.properties中配置数据库信息
spring.datasource.driver-class-name=com.mysql.jdbc.Driver(已弃用,用新的这个com.mysql.cj.jdbc.Driver)
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
5、dao层实现
package com.example.myspringbootdemo.dao;
import com.example.myspringbootdemo.DriverInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class DriverDaoImpl implements DriverDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public DriverInfo getById(Integer id) {
List<DriverInfo> driverInfoList=jdbcTemplate.query("select * from car_biz_driver_info where driver_id=?",new Object[]{id},
new BeanPropertyRowMapper(DriverInfo.class));
if(driverInfoList!=null && driverInfoList.size()>0){
DriverInfo driverInfo=driverInfoList.get(0);
return driverInfo;
}else{
return null;
}
}
}
四、整合mybatis
1、引入mybatis依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
2、引入数据库连接依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
3、引入数据源:
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver(com.mysql.cj.jdbc.Driver)
dao层用mapper注解:
@Mapper
public interface AccountMapper {
@Insert("insert into account(name, money) values(#{name}, #{money})")
int add(@Param("name") String name, @Param("money") double money);
@Update("update account set name = #{name}, money = #{money} where id = #{id}")
int update(@Param("name") String name, @Param("money") double money, @Param("id") int id);
@Delete("delete from account where id = #{id}")
int delete(int id);
@Select("select id, name as name, money as money from account where id = #{id}")
Account findAccount(@Param("id") int id);
@Select("select id, name as name, money as money from account")
List<Account> findAccountList();
}
这样,springboot就可以访问数据了。