Fork me on GitHub

ssm中逆向工程与分页的应用

昨天对springboot中的mybatis逆向工程与分页应用进行了整理,今天对ssm项目中的逆向工程与分页进行整理。
项目运行环境:eclipse+jdk1.8+maven+tomcat

一、搭建ssm项目

首先新建maven project,项目Archetype选择:maven-archetype-webapp,项目新建完毕,会出现生成的index.jsp报错。
造成原因:项目没有依赖javax.servlet相关类包,这些类包在tomcat类库中
有两种解决方案:
方法一:添加依赖


javax.servlet
servlet-api
2.5

方法二:依赖tomcat类库,前提是eclipse已经有tomcat服务器
依次 右击项目—>build path —> configure build path — >libraries — > add libraries —> Server Runtime — >选择tomcat服务器lib —> ok
项目建成依次添加依赖,代码如下:


<spring.version>4.3.7.RELEASE</spring.version>




junit
junit
3.8.1
test



javax.servlet
servlet-api
2.5



org.springframework
spring-core
${spring.version}


org.springframework
spring-beans
${spring.version}


org.springframework
spring-context
${spring.version}


org.springframework
spring-jdbc
${spring.version}


org.springframework
spring-tx
${spring.version}


org.springframework
spring-web
${spring.version}


org.springframework
spring-webmvc
${spring.version}


org.springframework
spring-test
${spring.version}
test



org.mybatis
mybatis
3.4.2


org.mybatis
mybatis-spring
1.3.1



com.github.pagehelper
pagehelper
5.1.2



mysql
mysql-connector-java
5.1.37



c3p0
c3p0
0.9.1.2


maven install下载好依赖包。
在src/main/resources文件夹下添加文件jdbc.properties,代码如下:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
在src/main/resources文件夹下添加文件mybatis-config.xml,代码如下:












在src/main/resources文件夹下添加文件夹spring,在spring中添加文件spring-dao.xml,代码如下:





<context:property-placeholder location="classpath:jdbc.properties"/>


































在spring中添加文spring-service.xml,代码如下:




<context:component-scan base-package="com.luis.service" />






<tx:annotation-driven transaction-manager="transactionManager" />

在spring中添加文spring-web.xml,代码如下:





<mvc:annotation-driven />


<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:default-servlet-handler />








<context:component-scan base-package="com.luis.web" />

修改web.xml文件,代码如下:



spring-dispatcher
org.springframework.web.servlet.DispatcherServlet

contextConfigLocation
classpath:spring/spring-*.xml


spring-dispatcher
/


index.jsp
index.html


至此,ssm项目搭建完毕!

二、逆向工程与分页应用

首先导入逆向工程项目,具体使用方法参见:我的github
本文仍旧应用上篇的数据库,数据库表如下:

DROP TABLE IF EXISTS user;
CREATE TABLE user (
id bigint(20) NOT NULL,
name varchar(255) NOT NULL,
age int(4) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO user VALUES ('1', 'wanger', '22');
INSERT INTO user VALUES ('2', 'zhangsan', '18');
INSERT INTO user VALUES ('3', 'lisi', '23');
INSERT INTO user VALUES ('4', 'wangwu', '21');
运行文件生成mapper接口,实体类与xml文件。
分页已经添加依赖,可直接应用。


com.github.pagehelper
pagehelper
5.1.2

另外在spring-dao.xml文件的SqlSessionFactory的bean中添加:







helperDialect=mysql





编写service层,UserService代码如下:

public interface UserService {
User selectByName(String name);
List findAllUser(int pageNum, int pageSize);
}
UserServiceImpl代码如下:

@Service
public class UserServiceImpl implements UserService {

@Autowired
private UserMapper userMapper;

public User selectByName(String name) {
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andNameEqualTo(name);
List users = userMapper.selectByExample(example);
if (users != null && users.size() > 0) {
return users.get(0);
}
return null;
}

/**
* pageNum 开始页数
* pageSize 每页显示的数据条数
*/
public List findAllUser(int pageNum, int pageSize) {
//将参数传给方法实现分页
PageHelper.startPage(pageNum, pageSize);
UserExample example = new UserExample();
List list = userMapper.selectByExample(example);
return list;
}
}
编写web层,UserController代码如下:

@Controller
public class UserController {

@Autowired
private UserService userService;

@ResponseBody
@RequestMapping("/test")
public String querUserByName() {
User user = userService.selectByName("wanger");
System.out.println(user.toString());
return "index";
}

@RequestMapping("/list")
public String querUser() {
List list = userService.findAllUser(1, 2);
//获取分页信息
PageInfo pageInfo = new PageInfo(list);
System.out.println(list.toString());
System.out.println("total:" + pageInfo.getTotal());
System.out.println("pages:" + pageInfo.getPages());
System.out.println("pageSize:" + pageInfo.getPageSize());
return "index";
}
}

三、测试

将项目部署到tomcat,启动,访问 http://localhost:8080/ssm-demo/test ,运行结果:

User [id=1, name=wang1er, age=22]
逆向工程与ssm项目搭建成功!
访问 http://localhost:8080/ssm-demo/list ,运行结果:

Page{count=true, pageNum=1, pageSize=2, startRow=0, endRow=2, total=4, pages=2, reasonable=false, pageSizeZero=false}
total:4
pages:2
pageSize:2
此处遇到的坑:照着网上的很多例子添加了plugins,分页过程中出现错误:

警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in file [E:\code\eclipse-workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ssm-demo\WEB-INF\classes\spring\spring-dao.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.Object[]' to required type 'org.apache.ibatis.plugin.Interceptor[]' for property 'plugins'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'com.github.pagehelper.PageHelper' to required type 'org.apache.ibatis.plugin.Interceptor' for property 'plugins[0]': no matching editors or conversion strategy found
九月 06, 2018 10:51:00 下午 org.springframework.web.servlet.DispatcherServlet initServletBean
严重: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in file [E:\code\eclipse-workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ssm-demo\WEB-INF\classes\spring\spring-dao.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.Object[]' to required type 'org.apache.ibatis.plugin.Interceptor[]' for property 'plugins'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'com.github.pagehelper.PageHelper' to required type 'org.apache.ibatis.plugin.Interceptor' for property 'plugins[0]': no matching editors or conversion strategy found
花了很大时间,最后在https://blog.csdn.net/wangyuanjun008/article/details/79121459
找到了解决方案。将plugins改为:







helperDialect=mysql





项目测试完毕!
具体的项目代码,参见于:github

本文参考了:
https://blog.csdn.net/wangyuanjun008/article/details/79121459

posted @ 2018-09-06 23:22  紫焱luis  阅读(569)  评论(0)    收藏  举报