SpringBoot使用篇(SSM框架)
项目结构图如下:
提前说明:
①springBoot简化了很多以往SSM框架的配置,提倡使用0个xml文件,所以用起来很方便,下面讲解如何使用。
②最重要的内容都在application.properties文件中。
③mybatis如何与spring连接起来(mapper映射):启动类上会扫描mapper.java接口文件;mapper.xml文件在启动的时候被加载,配置命名空间指向mapper.java接口文件。
1、新建maven的quickstart工程,并且在pom文件中引入各种依赖
<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>cn.learn</groupId>
<artifactId>SpringBootTest1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringBootTest1</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入mybatis依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.14</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
</project>
2、编写启动类
①本类启动的时候会扫描和这个类同包及其子孙包下的所有类,只要类上有注解,那么就会变成一个bean,可以供注入使用;
②所以启动类的包名是cn.learn,别的类的包名是cn.learn.con…
@SpringBootApplication
@MapperScan("cn.learn.mapper")
public class SpringBootStarter1 {
public static void main(String[] args) {
SpringApplication.run(SpringBootStarter1.class, args);
}
}
3、编写controller层
@RestController
public class ControllerTest {
@Autowired
private ServiceTest service=null;
@RequestMapping("/userquery")
public String queryUserByName(String username) {
return service.queryUser(username);
}
}
4、编写service层
@Service
public class ServiceTest {
@Autowired
Mapper mapper=null;
public String queryUser(String username) {
User user=mapper.queryUser(username);
if(user!=null) {
return user+"存在";
}else {
return "不存在哦";
}
}
}
5、编写mapper层
public interface Mapper {
User queryUser(String username);
}
6、编写mapper.xml配置文件
①这里的namespace指向的是mapper.java文件(那个接口);
②id是mapper.java文件中的方法名;
③resultType简写了,因为在application.properties文件中配置了别名空间;
④#{username}中的username是传过来的参数名,必须和接口中方法的参数名对应上。
<?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="cn.learn.mapper.Mapper">
<select id="queryUser" parameterType="string" resultType="User">
select * from user where name=#{username};
</select>
</mapper>
7、application.properties文件
#本机启动访问 localhost:8091/springboot01 才会到这个工程
#端口号
server.port=8091
#tomcat发布路径
server.contextPath=/springboot01
#datasource配置
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql:///lee
spring.datasource.username=root
spring.datasource.password=123456
#mybatis配置
#别名包,sql语句返回的对象封装到entity中
mybatis.typeAliasesPackage=cn.learn.entity
#会去src/main/resources路径下找xml配置文件
mybatis.mapperLocations=classpath:mapper/*.xml
mybatis.configuration.mapUnderscoreToCamelCase=true
mybatis.configuration.cacheEnabled=false