springboot的优势之一就是快速搭建项目,省去了自己导入jar包和配置xml的时间,使用非常方便。
1、选择file>new>project创建项目project,然后选择Spring initializr,点击下一步
2、按图示进行勾选,点击下一步,给项目起个名字,点击确定。
3、选择左边的列web,右边列勾选web框
4、如果前面没有修改工程名称,可以在这一界面更改,然后点击finish。
5、项目开始生成,界面右下角回弹出一个小方框,点击Enable Autop-Import ,idea 会自动下载jar包,时间比较长 (5分钟左右)
6、等待片刻后,项目格式如下图。
7、构建pom
xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0modelVersion> <groupId>com.examplegroupId> <artifactId>demoartifactId> <version>0.0.1-SNAPSHOTversion> <packaging>jarpackaging> <name>demoname> <description>Demo project for Spring Bootdescription> <parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.0.3.RELEASEversion> <relativePath/> parent> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding> <java.version>1.8java.version> properties> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> <dependency> <groupId>org.mybatis.spring.bootgroupId> <artifactId>mybatis-spring-boot-starterartifactId> <version>1.1.1version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <scope>runtimescope> dependency> <dependency> <groupId>com.alibabagroupId> <artifactId>druid-spring-boot-starterartifactId> <version>1.1.0version> dependency> dependencies> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build> project>
8、创建application.yml文件
server: port: 8080 spring: datasource: name: test driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://192.168.3.164:3307/kuangke?useUnicode=true&characterEncoding=utf-8&useSSL=true username: root password: root # 使用druid数据源 type: com.alibaba.druid.pool.DruidDataSource filters: stat maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 'x' testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20 mybatis: mapper-locations: classpath*:mybatis/mapper/*.xml
9、user实体类
public class User { private String id; private String name; private String sex; private String age ; private String memo; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getMemo() { return memo; } public void setMemo(String memo) { this.memo = memo; } }
10 、创建Usermapper 接口
//@Mapper //声明是一个Mapper,与springbootApplication中的@MapperScan二选一写上即可 @Repository public interface UserMapper { ListgetAllUser(); }
11、创建接口 UserService
public interface UserService { ListgetAllUser(); }
12 、创建UserServiceImpl
@Service(value = "userService") public class UserServiceImpl implements UserService { @Resource private UserMapper userMapper; @Override public ListgetAllUser() { return userMapper.getAllUser(); } }
13 、在resource目录下创建mybatis文件下、在该文件下创建mapper文件夹、然后在该文件夹下创建UserMapper.xml
xml version="1.0" encoding="UTF-8"?> mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.yangkai.mapper.UserMapper"> <select id="getAllUser" resultType="com.yangkai.entity.User"> SELECT * FROM user select> mapper>
14 、创建controller
@Controller @RequestMapping(value = "/user") public class UserController { @Autowired private UserService userService; @RequestMapping("/getAllUser") @ResponseBody private ListgetAllUser() { List users = userService.getAllUser(); return users; } }
15、最后附上启动器的代码
@SpringBootApplication @EnableTransactionManagement //开启事务管理 @ComponentScan("com.yangkai") @MapperScan("com.yangkai.mapper")//与dao层的@Mapper二选一写上即可(主要作用是扫包) public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Bean(destroyMethod = "close", initMethod = "init") @ConfigurationProperties(prefix = "spring.datasource") public DataSource druidDataSource() { DruidDataSource druidDataSource = new DruidDataSource(); return druidDataSource; } }
16、项目结构图
17、启动项目在浏览器输入:http://localhost:8080/user/getAllUser ,如下图
源码下载:https://download.csdn.net/download/qq_36245532/10515363