STS MVC与MyBatis的结合

1. MVC关键点在于Controller

1.1 Controller通过返回两种类型的数据完成用户端请求的回复:一种是模型(视图),另一种是JSON数据。

1.2 Controller类采用@Controller标签,可以加上RequestMapping,用于适配器解析;

1.3Controller类中用于处理客户请求的方法必须带有@RequestMapping标签,提供前后端参数名称的映射关系(即HTTP参数与方法参数的映射关系);

   每个方法的输入参数需要带有@RequestParam标签,如:@RequestParam(value = "userName", required = false) String userName

   下面是一个完整的方法:

@RequestMapping("/list")
@ResponseBody
public List<User> list( @RequestParam(value = "userName", required = false) String userName,  @RequestParam(value = "note", required = false) String note) {
// 访问模型层得到数据
List<User> userList = userService.findUsers(userName, note);
return userList;
}

一种情况可以不带@RequestParam标签,即当输入参数与HTTP本来就一致时。

2. 生成服务

   Controller中可能会有一个或多个服务,如果要采用MyBatis,需要进行封装,即:

  (1)生成一个带有服务所有操作的接口,接口参数需要带有@Mapper标签,建立与mapper.xml中的SQL变量名的映射关系

@Mapper
public interface UserDao {

User getUser(Long id);

List<User> findUsers(@Param("userName") String userName, @Param("note") String note);
}

  (2)通过XML文件建立方法与SQL的映射关系

<?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.springboot.chapter9.dao.UserDao">

<select id="findUsers" resultType="user">
select id, user_name as userName, note from t_user
<where>
<if test="userName != null"> and user_name like concat('%', #{userName}, '%')</if>
<if test="note != null"> and note like concat('%', #{note}, '%')</if>
</where>
</select>
</mapper>

这里id="findUsers",指出了控制器中的函数名,resultType="user"指出了返回的值类型,输入的值类型这里省略了(可省略吗?)

这里的#{userName}对应了@Param("userName"),#{note}对应了@Param("note") ,SQL语句中的user_name as userName将数据库列项与输入参数相关联,保证将来数据读取的正确性。

这里还有一个疑问:findUsers返回的值是List<User>,但在XML文件中对应的返回会师却是:resultType="user"。这里的userUser类的别名,由类前面加@Alias标签确定。

(3)建立一个接口用于定义服务,其内部函数应当与前面的@Mapper接口完全一致,但不再需要@Param标签;

(4)建立一个服务类实现前面的服务接口,必须加入@Service标签,如果是事务的话,

需要加入@Transactional标签。类中要声明一个@Mapper定义的接口类型成员,并用@Autowired标识为自动生成,然后实现其中的方法,方法中调用@Mapper接口的函数:

@Service
@Transactional
public class UserServiceImpl implements UserService {

@Autowired
private UserDao userDao = null;

@Override
public User getUser(Long id) {
return userDao.getUser(id);
}

@Override
public List<User> findUsers(String userName, String note) {
return userDao.findUsers(userName, note);
}
}

3.完成扫描和配置(bean和mapper)

@SpringBootApplication(scanBasePackages = "com.springboot.chapter9")
@MapperScan(basePackages="com.springboot.chapter9", annotationClass = Mapper.class)
public class Chapter9Application {
// @Autowired
// SqlSessionFactory sqlSessionFactory = null;
//
// @Bean
// public MapperFactoryBean<UserDao> initMyBatisUserDao() {
// MapperFactoryBean<UserDao> bean = new MapperFactoryBean<>();
// bean.setMapperInterface(UserDao.class);
// bean.setSqlSessionFactory(sqlSessionFactory);
// return bean;
// }
public static void main(String[] args) {
SpringApplication.run(Chapter9Application.class, args);
}
}

 在程序主类中加入bean和mapper的扫描范围,这里不再需要加入SqlSessionFactory这一类的操作,可能是框架已经做到了?

4. 添加相关的依赖项和配置

<?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>springboot</groupId>
<artifactId>chapter9</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>chapter9</name>
<description>chapter9 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-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--配置对mybatis的依赖项,这两个必须有,少一个就不能通过 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>

<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</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>

(1)原例中使用了:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

删除后没有问题,所以去掉。

(2)生成后运行时,在页面中出现了 <%@ page pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>,这是JSP文件的上方的两行与JSTS相关的,不知为什么没能解析

(3)运行还是出错,后将mysql-connector-java依赖版本改为8.0.11,数据库操作正确,但仍找不到jsp资源;

(4)由于运行中使用了easyui的前端框架,开始时无法显示结果,但发现不是服务的原因,而是对jsp资源的解析问题。通过查资料,加入thymeleaf依赖;在application.properties文档中加入:

spring.thymeleaf.prefix=classpath:/WEB-INF/jsp/
spring.thymeleaf.suffix=.jsp
spring.thymeleaf.mode=LEGACYHTML5
#spring.thymeleaf.encoding=UTF-8
#spring.thymeleaf.content-type=text/html # ;charset=<encoding> is added

spring.thymeleaf.cache=false

   结果正确,但起始没有数据,然后在相应的jsp中加入了

<script type="text/javascript">
// 定义事件方法
$(document).ready(function() {
// 指定请求路径
var opts = $("#dg").datagrid("options");
opts.url = "./list";
// 获取查询参数
var userName = "user_name";
var note = "note";
// 组织参数
var params = {};
if (userName != null && userName.trim() != '') {
params.userName = userName;
}
if (note != null && note.trim() != '') {
params.note = note;
}
// 重新载入表格数据
$("#dg").datagrid('load', params);
});
</script>

数据显示出来。

(5)其他配置

(5.1)数据库配置

spring.datasource.url=jdbc:mysql://localhost:3306/user
spring.datasource.username=root
spring.datasource.password=123456

#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.tomcat.max-idle=10
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.max-wait=10000
spring.datasource.tomcat.initial-size=5

  (5.2)Mybatis配置

    #mapper.xml所在目录

     mybatis.mapper-locations=classpath:com/springboot/chapter9/mapper/*.xml 

    #POJO类包名

mybatis.type-aliases-package=com.springboot.chapter9.pojo

(5.3)由于有了thymeleaf的配置,下面的配置不再有效:

#有了thymeleaf设置,这里将不再需要
#spring.mvc.view.prefix=/WEB-INF/jsp/
#spring.mvc.view.suffix=.jsp

5. 需要深入了解的问题:

  6.1 easyui是一个基于JQuery创建的客户端框架,它的用法还需要了解。其界面比较好,所以可以学一下;

  6.2 thymeleaf是一个用于解析jsp的框架,现在一无所知,需要了解

猜你喜欢

转载自www.cnblogs.com/myboat/p/11526960.html
sts
今日推荐