学习Spring Boot框架,想必已经感受到该框架带来的快感,接下来教你如何快速搭建Spring、SpringMvc、Mybatis框架整合。

1.项目构建


2.sql文件
/* MySQL Backup Source Server Version: 5.7.13 Source Database: springboot
Date: 2018/3/14 10:28:39 */ SET FOREIGN_KEY_CHECKS=0; --
---------------------------- -- Table structure for `user` --
---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` (
`id` int(11) NOT NULL COMMENT '主键', `name` varchar(20) DEFAULT NULL, `birthday`
date DEFAULT NULL COMMENT '生日', `address` varchar(256) DEFAULT NULL COMMENT '地址'
,PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; --
---------------------------- -- Records -- ---------------------------- INSERT
INTO `user` VALUES ('1','张三','1994-10-20','杭州市'), ('2','李四','1996-02-19','上海市');
3.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.0</modelVersion> <groupId>com.example</groupId> <artifactId>
demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <
name>demo</name> <description>Demo project for Spring Boot</description> <parent
> <groupId>org.springframework.boot</groupId> <artifactId>
spring-boot-starter-parent</artifactId> <version>2.0.0.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-web</artifactId> </dependency> <dependency> <groupId>
org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</
artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>
org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</
artifactId> </dependency> <dependency> <groupId>org.springframework.boot</
groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </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> </dependencies> <build> <plugins>
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>
spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
4.application.properties文件
#database 数据源 spring.datasource.url=jdbc:mysql://localhost:3306
/springboot?useUnicode=true&characterEncoding=utf8 spring.datasource.username
=root spring.datasource.password=123456 spring.datasource.driver-class-name=com
.mysql.jdbc.Driver #tomcat端口 server.port=8080 #整合mybatis
#起别名,省略写mybatis的xml中的resultType的全路径 mybatis.mapper-locations = classpath:mapper
/*Mapper.xml #扫描(配置xml模式使用) mybatis.type-aliases-package=com.example.pojo
5.mybatis的映射文件UserMapper.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.mapper.IUserMapper"> <resultMap type="User" id="UserList"> <result
column="id" property="id" /> <result column="name" property="name" /> <result
column="birthday" property="birthday" /> <result column="address" property=
"address" /> </resultMap> <select id="queryAllUser" resultMap="UserList">
SELECT * FROM user</select> <delete id="deleteUser" parameterType="int"> DELETE
FROM user WHERE id = #{id}</delete> </mapper>
6.数据交互层Controller
@RestController @EnableAutoConfiguration public class IndexController {
@Autowired private IUserService userService; /** * 整合SSM框架 */ @RequestMapping(
"/ssm") public List<User> findAllUser() throws Exception{ return
userService.queryAllUser(); }@RequestMapping(value = "/delete/{id}",method =
RequestMethod.GET)public void findAllUser(@PathVariable Integer id) throws
Exception{ userService.deleteUser(id); }/** * 返回基本格式JSON格式数据 * @return */
@RequestMapping(value="/index",produces="text/plain;charset=UTF-8") public
Stringindex(){ return "hello spring boot"; } /** * 返回POJO对象 * @return */
@RequestMapping("/pojo") public User showUser(){ User user = new User();
user.setId(1); user.setName("张三"); user.setBirthday("1990-02-20");
user.setAddress("武当山"); return user; } /** * 返回Map集合对象 */ @RequestMapping("/map"
)public Map<String, Object> showMap(){ Map<String, Object> map = new
HashMap<String, Object>(); map.put("username", "张三丰"); map.put("gender", "男");
map.put("username", "赵敏"); map.put("gender", "女"); return map; } /** *
返回List集合对象 */ @RequestMapping("/list") public List<User> showList(){ List<User>
list =new ArrayList<User>(); User u1 = new User(); u1.setId(1); u1.setName("张三"
); u1.setAddress("武当山"); u1.setBirthday("1990-02-20"); User u2 = new User();
u2.setId(2); u2.setName("李四"); u2.setAddress("上海市"); u2.setBirthday("1990-02-21"
); list.add(u1); list.add(u2);return list; } }
7.业务逻辑层Service
public interface IUserService { List<User> queryAllUser() throws Exception;
void deleteUser(Integer id) throws Exception; } @Service public class
UserServiceImpl implements IUserService{ @Autowired private IUserMapper mapper;
@Override public List<User> queryAllUser() throws Exception { return
mapper.queryAllUser(); }@Override public void deleteUser(Integer id) throws
Exception { mapper.deleteUser(id); } }
8.数据持久层及实体Bean
//@Mapper //声明是一个Mapper,与DemoApplication中的@MapperScan二选一写上即可 public interface
IUserMapper { List<User> queryAllUser() throws Exception; void
deleteUser(Integer id)throws Exception; } public class User implements
Serializable { private Integer id; private String name; private String birthday;
private String address; //省略getXxx()、setXxx() }
9.入口类
@MapperScan("com.example.mapper") @SpringBootApplication
@EnableTransactionManagement//启注解事务管理,等同于xml配置方式的 <tx:annotation-driven />
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args); } }
运行结果

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信