SpringBoot+SpringMvc+MyBatis整合
1、使用maven或sts创建springBoot项目
2、pom.xml配置
<?xml version="1.0" encoding="UTF-8"?> <modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hp</groupId>
<artifactId>springBoot_010</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springBoot_010</name>
<description>hello world springBoot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- mybatis启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- mysql数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid数据库连接池 -->
<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>
3、创建application.properties或application.yml
#使用application.properties配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis.type-aliases-package=com.hp.pojo
mybatis.configuration.map-underscore-to-camel-case=true
#使用application.yml配置
spring:
thymeleaf:
cache: false
encoding: UTF-8
enable-spring-el-compiler: true
mode: HTML5
datasource:
url: localhost:3306/mybatis?useSSL=false
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
mybatis:
type-aliases-package: com.hp.pojo
configuration.map-underscore-to-camel-case: true
4、创建tbluser表
DROP TABLE IF
EXISTS tbluser
;
CREATE TABLE
tbluser
(
user_id
int(11) NOT NULL AUTO_INCREMENT,
user_name
varchar(20) DEFAULT NULL
COMMENT ‘用户名’,
pass_word
varchar(20) DEFAULT NULL
COMMENT ‘密码’,
create_date
datetime DEFAULT NULL,
address
varchar(100) DEFAULT NULL,
PRIMARY KEY (user_id
)
) ENGINE=InnoDB
AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
4、创建User类
public class User {
private Integer userId;
private String userName;
private String passWord;
private Date createDate;
private String address;
省略getter(),setter()方法
}
5、创建UserMapper接口
public interface UserMapper {
/**
* 查询所有用户信息
* @return
*/
public List<User> selectUsers();
/**
* 添加用户信息
*/
public void insertUser(User user);
}
6、创建sql映射文件
<?xml version="1.0" encoding="UTF-8" ?><!-- 查询所有用户信息
–>
selec *
from tbluser u
<!-- 添加用户信息
–>
insert into
tbluser(user_name,pass_word,address,create_date)
values(#{userName},#{passWord},#{address},#{createDate})
7、创建service层接口
public interface IUserService {
/**
* 查询所有用户信息
* @return
*/
public List<User> selectUsers();
/**
* 添加用户信息
*/
public void insertUser(User user);
}
8、创建service层接口的实现类
@Service
@Transactional
public class UserService implements IUserService {
@Resource
private UserMapper userMapper;
/**
* 查询所有用户信息
* @return
*/
public List<User> selectUsers(){
return
userMapper.selectUsers();
}
/**
* 添加用户信息
*/
public void insertUser(User user) {
userMapper.insertUser(user);
}
}
9、创建UserController类
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private IUserService userService;
/**
* 进入新增页面
* @param map
* @return
*/
@RequestMapping(value="userEdit")
public String userEdit(ModelMap map) {
return
“user/userEdit”;
}
/**
* 保存用户信息
*/
@RequestMapping(value="addUser")
public String addUser(User user,ModelMap
map) {
userService.insertUser(user);
return
“redirect:userInfo”;
}
/**
* 查询所有用户信息
*/
@RequestMapping(value="userInfo")
public String userInfo(ModelMap map) {
List<User> userList = userService.selectUsers();
map.put("userList", userList);
return
“user/userInfo”;
}
10、创建新增用户页面UserEdit.html
用户密码:
用户地址:
创建日期:
11、新增显示列表页面userInfo.html
用户编号 | 用户姓名 | 用户密码 | 用户地址 | 创建日期 |
---|---|---|---|---|
12、创建启动类
@SpringBootApplication
@MapperScan(value=“com.hp.mapper”)
public class App {
public static void
main(String[] args) {
SpringApplication.run(App.class, args);
}
@Bean
public Converter<String, Date> dateFormatConvert()
{
return new Converter<String, Date>() {
@Override
public Date convert(String source)
{
SimpleDateForm
SpringBoot+SpringMvc+MyBatis整合
1、使用maven或sts创建springBoot项目
2、pom.xml配置
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
com.hp
springBoot_010
0.0.1-SNAPSHOT
springBoot_010
hello world springBoot
<!-- jdk版本 -->
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--
springBoot启动器 -->
org.springframework.boot
spring-boot-starter-web
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- mybatis启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- thymeleaf
–>
org.springframework.boot
spring-boot-starter-thymeleaf
<!-- mysql数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid数据库连接池 -->
<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>
3、创建application.properties或application.yml
#使用application.properties配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis.type-aliases-package=com.hp.pojo
mybatis.configuration.map-underscore-to-camel-case=true
#使用application.yml配置
spring:
thymeleaf:
cache: false
encoding: UTF-8
enable-spring-el-compiler: true
mode: HTML5
datasource:
url: localhost:3306/mybatis?useSSL=false
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
mybatis:
type-aliases-package: com.hp.pojo
configuration.map-underscore-to-camel-case: true
4、创建tbluser表
DROP TABLE IF
EXISTS tbluser
;
CREATE TABLE
tbluser
(
user_id
int(11) NOT NULL AUTO_INCREMENT,
user_name
varchar(20) DEFAULT NULL
COMMENT ‘用户名’,
pass_word
varchar(20) DEFAULT NULL
COMMENT ‘密码’,
create_date
datetime DEFAULT NULL,
address
varchar(100) DEFAULT NULL,
PRIMARY KEY (user_id
)
) ENGINE=InnoDB
AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
4、创建User类
public class User {
private Integer userId;
private String userName;
private String passWord;
private Date createDate;
private String address;
省略getter(),setter()方法
}
5、创建UserMapper接口
public interface UserMapper {
/**
* 查询所有用户信息
* @return
*/
public List<User> selectUsers();
/**
* 添加用户信息
*/
public void insertUser(User user);
}
6、创建sql映射文件
<?xml version="1.0" encoding="UTF-8" ?><!-- 查询所有用户信息
–>
select
*
from tbluser u
<!-- 添加用户信息
–>
insert into
tbluser(user_name,pass_word,address,create_date)
values(#{userName},#{passWord},#{address},#{createDate})
7、创建service层接口
public interface IUserService {
/**
* 查询所有用户信息
* @return
*/
public List<User> selectUsers();
/**
* 添加用户信息
*/
public void insertUser(User user);
}
8、创建service层接口的实现类
@Service
@Transactional
public class UserService implements IUserService {
@Resource
private UserMapper userMapper;
/**
* 查询所有用户信息
* @return
*/
public List<User> selectUsers(){
return
userMapper.selectUsers();
}
/**
* 添加用户信息
*/
public void insertUser(User user) {
userMapper.insertUser(user);
}
}
9、创建UserController类
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private IUserService userService;
/**
* 进入新增页面
* @param map
* @return
*/
@RequestMapping(value="userEdit")
public String userEdit(ModelMap map) {
return
“user/userEdit”;
}
/**
* 保存用户信息
*/
@RequestMapping(value="addUser")
public String addUser(User user,ModelMap
map) {
userService.insertUser(user);
return
“redirect:userInfo”;
}
/**
* 查询所有用户信息
*/
@RequestMapping(value="userInfo")
public String userInfo(ModelMap map) {
List<User> userList = userService.selectUsers();
map.put("userList", userList);
return
“user/userInfo”;
}
10、创建新增用户页面UserEdit.html
用户密码:
用户地址:
创建日期:
11、新增显示列表页面userInfo.html
用户编号 | 用户姓名 | 用户密码 | 用户地址 | 创建日期 |
---|---|---|---|---|
12、创建启动类
@SpringBootApplication
@MapperScan(value=“com.hp.mapper”)
public class App {
public static void
main(String[] args) {
SpringApplication.run(App.class, args);
}
@Bean
public Converter<String, Date> dateFormatConvert()
{
return new Converter<String, Date>() {
@Override
public Date convert(String source)
{
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
Date date = null;
try {
date = sdf.parse((String) source);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
};
}
}
at sdf = new SimpleDateFormat(“yyyy-MM-dd”);
Date date = null;
try {
date = sdf.parse((String) source);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
};
}
}