一、展示搭建好的框架目录文件
pom.xml文件:存放maven依赖有关的配置。
application.yml文件:整个工程的配置文件,这里用了.yml文件,比.properties文件的语言更简洁,配置代码结构更有层次感。
user.xml文件:user实体类数据库操作的配置文件,提供给Dao层接口调用。
DemoApplication.java文件:springBoot的工程启动入口类。
user.java、userDao.java、BootController.java:是后台开mvc经典分层开发的对应类(实体类,数据库访问,控制层)
二、创建一个springBoot的maven工程
1.新建一个springBoot的maven工程:
File-->new-->project-->(一般情况下)next进入如下界面:
填写相应的group和Artifact,group一般为网址的反写,注意Artifact的内容都要小写,然后点击next,进入如下界面:
点击左边依赖列表选择和右边红色框内相同名字的依赖,点击next,进入如下界面。
点击finish,这样就成功创建了一个maven工程。
三、完成具体文件配置
1.新建好的工程目录如下:
这个时候我们首先要点击打开pom.xml,加入
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId>
<version>1.1.0</version> </dependency>依赖,加入后如下:
<?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.0</modelVersion> <groupId>com.example.second</groupId>
<artifactId>demosecond</artifactId> <version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>demosecond</name> <description>Demo project
for Spring Boot</description> <parent> <groupId>org.springframework.boot
</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>
2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository -->
</parent> <properties> <project.build.sourceEncoding>UTF-8
</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8
</project.reporting.outputEncoding> <java.version>1.8</java.version>
</properties> <dependencies> <dependency> <groupId>org.springframework.boot
</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>
spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web
</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot
</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2
</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>
mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>
spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId>
<version>1.1.0</version> </dependency></dependencies> <build> <plugins>
<plugin> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins>
</build> </project>
然后新建一个application.yml文件,文件的路径如文件目录所示,编辑如下内容:
#开发配置 spring: #数据源配置 datasource: url:
jdbc:mysql://localhost:3306/smin?useUnicode=true&characterEncoding=utf-8&useSSL=false
username:root password: root123 driver-class-name: com.mysql.jdbc.Driver type:
com.alibaba.druid.pool.DruidDataSource #公共配置与profiles选择无关 mybatis:
typeAliasesPackage:com.example.second.demosecond.entity mapperLocations:
classpath:mapper/*.xml
然后在DemosecondApplication.java中加入
@MapperScan("com.example.second.demosecond.dao")注解即可。
至此文件配置工作已经完成(注意url,username,password字段,mysql要连接你们自己的)。
四、进行测试开发
1.在mysql数据库中创建user表,结构如图:
2.在src\main\resources目录下创建mapper文件,并在mapper文件夹中创建user.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
="com.example.second.demosecond.dao.UserDao"> <resultMap id="BaseResultMap"
type="com.example.second.demosecond.entity.User" > <id column="id" property
="id"jdbcType="INTEGER" /> <result column="name" property="userName" jdbcType
="VARCHAR"/> <result column="address" property="address" jdbcType="VARCHAR" />
</resultMap> <sql id="Base_Column_List" > id, name, address </sql> <select id
="selectUser"resultMap="BaseResultMap"> select <include refid="Base_Column_List"
/> from user </select> </mapper>
3.在DemosecondApplication.java的平级目录下创建entity,dao,controller这个文件夹。
(1)在entity文件夹中创建User.java,编辑如下代码:
public class User { private String UserName; private Integer id; private
Stringaddress; public Integer getId() { return id; } public void setId(Integer
id) {this.id = id; } public String getAddress() { return address; } public void
setAddress(String address) { this.address = address; } public String getUserName
() {return UserName; } public void setUserName(String userName) { UserName =
userName; } }
(2)在dao文件夹中创建UserDao.java文件,内容如下:
import com.example.second.demosecond.entity.User; import
org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public
interfaceUserDao { List<User> selectUser(); }
(3)在controller文件夹中创建BootController.java文件,其内容如下:
import com.example.second.demosecond.dao.UserDao; import
com.example.second.demosecond.entity.User; import
org.springframework.beans.factory.annotation.Autowired; import
org.springframework.boot.autoconfigure.EnableAutoConfiguration; import
org.springframework.web.bind.annotation.RequestMapping; import
org.springframework.web.bind.annotation.RestController; import java.util.List;
@RestController@EnableAutoConfiguration @RequestMapping(value = "/boot") public
classBootController { @Autowired private UserDao userDao; @RequestMapping(value
="getUser") public User getUser() { User user=new User(); user.setUserName(
"test"); return user; } @RequestMapping(value = "getAllUser") public List<User>
getUsers() { List<User> userList= userDao.selectUser(); return userList; } }
4.点击运行DemosecondApplication.java,在地址栏中输入:http://localhost:8080/boot/getAllUser
即可以得到如下数据库访问结果:
热门工具 换一换