SpringBoot入门
- 1.导入maven包
- 2.在resources根目录下配置application.propertie或者application.yml
- 3.新建cn.itsource包,然后在写一个Application.java文件
- 4.新建cn.itsource.model,并写一个实体类Country.java
- 5.新建cn.itsource.mapper包并写CountryMapper.java
- 6.早resources下面创建cn.itsource.mapper并创建映CountryMapper.xml
- 7.最后在mysql里面新建一个数据库mybatis并新建一张表country,添加数据,然后启动Application里面的main方法,然后就能打印出来
1.导入maven包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>
<!-- jdk版本 -->
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- springboot启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- jdbc驱动 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- mybatis集成springboot -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
<!-- maven支持插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.在resources根目录下配置application.propertie或者application.yml
建议使用yml,因为yml具有层次感,也是比较潮流的
# mysql四大金刚
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
spring.datasource.username=root
spring.datasource.password=
# 配置xml的路径
mybatis.mapper-locations=classpath*:cn/itsource/mapperr/*.xml
# 实体类的别名
mybatis.type-aliases-package=cn.itsource.model
3.新建cn.itsource包,然后在写一个Application.java文件
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
private CountryMapper countryMapper;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
//实现CommandLineRunner 接口,重写的方法会在tomcat启动之后立即运行
@Override
public void run(String... args) throws Exception {
List<Country> selectAll = countryMapper.selectAll();
for (Country country : selectAll) {
System.out.println(country);
}
}
4.新建cn.itsource.model,并写一个实体类Country.java
public class Country {
private Long id;
private String countryname;
private String countrycode;
//getter setter
}
5.新建cn.itsource.mapper包并写CountryMapper.java
@mapper就会自动扫描,如果是@repository或者@service或者是@component@controller,那么就需要在Application启动类上配置扫描注解@MapperScan(“cn.itsource.mapper”)
@Mapper
public interface CountryMapper {
/**
* 查询全部数据
*
* @return
*/
List<Country> selectAll();
}
6.早resources下面创建cn.itsource.mapper并创建映CountryMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="tk.mybatis.springboot.mapper.CountryMapper">
<select id="selectAll" resultType="Country">
select id,countryname,countrycode from country
</select>
</mapper>