ssm中逆向工程与分页的应用
昨天对springboot中的mybatis逆向工程与分页应用进行了整理,今天对ssm项目中的逆向工程与分页进行整理。
项目运行环境:eclipse+jdk1.8+maven+tomcat
搭建ssm项目
首先新建maven project,项目archetype选择:maven-archetype-webapp,项目新建完毕,会出现生成的index.jsp报错。
造成原因:项目没有依赖javax.servlet相关类包,这些类包在tomcat类库中
有两种解决方案:
方法一:添加依赖
<dependency> <groupid>javax.servlet</groupid> <artifactid>servlet-api</artifactid> <version>2.5</version> </dependency>
方法二:依赖tomcat类库,前提是eclipse已经有tomcat服务器
依次 右击项目—>build path —> configure build path — >libraries — > add libraries —> server runtime — >选择tomcat服务器lib —> ok
项目建成依次添加依赖,代码如下:
<properties> <spring.version>4.3.7.release</spring.version> </properties> <dependencies> <!-- 单元测试 --> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- 解决新建jsp文件报错 --> <dependency> <groupid>javax.servlet</groupid> <artifactid>servlet-api</artifactid> <version>2.5</version> </dependency> <!-- spring包 --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-core</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-beans</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-jdbc</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-tx</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-web</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-test</artifactid> <version>${spring.version}</version> <scope>test</scope> </dependency> <!-- mybatis --> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis</artifactid> <version>3.4.2</version> </dependency> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis-spring</artifactid> <version>1.3.1</version> </dependency> <!-- 分页 --> <dependency> <groupid>com.github.pagehelper</groupid> <artifactid>pagehelper</artifactid> <version>5.1.2</version> </dependency> <!-- 数据库 --> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>5.1.37</version> </dependency> <!-- c3p0连接池 --> <dependency> <groupid>c3p0</groupid> <artifactid>c3p0</artifactid> <version>0.9.1.2</version> </dependency> </dependencies>
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,代码如下:
<?xml version="1.0" encoding="utf-8"?> <!doctype configuration public "-//mybatis.org//dtd config 3.0//en" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 配置全局属性 --> <settings> <!-- 使用jdbc的getgeneratedkeys获取数据库自增主键值 --> <setting name="usegeneratedkeys" value="true" /> <!-- 使用列标签替换列别名 默认:true --> <setting name="usecolumnlabel" value="true" /> <!-- 开启驼峰命名转换:table{create_time} -> entity{createtime} --> <setting name="mapunderscoretocamelcase" value="true" /> </settings> </configuration>
在src/main/resources文件夹下添加文件夹spring,在spring中添加文件spring-dao.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置整合mybatis过程 --> <!-- 1.配置数据库相关参数properties的属性:${url} --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 2.数据库连接池 --> <bean id="datasource" class="com.mchange.v2.c3p0.combopooleddatasource"> <!-- 配置连接池属性 --> <property name="driverclass" value="${jdbc.driver}" /> <property name="jdbcurl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- c3p0连接池的私有属性 --> <property name="maxpoolsize" value="30" /> <property name="minpoolsize" value="10" /> <!-- 关闭连接后不自动commit --> <property name="autocommitonclose" value="false" /> <!-- 获取连接超时时间 --> <property name="checkouttimeout" value="10000" /> <!-- 当获取连接失败重试次数 --> <property name="acquireretryattempts" value="2" /> </bean> <!-- 3.配置sqlsessionfactory对象 --> <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean"> <!-- 注入数据库连接池 --> <property name="datasource" ref="datasource" /> <!-- 配置mybaties全局配置文件:mybatis-config.xml --> <property name="configlocation" value="classpath:mybatis-config.xml" /> <!-- 扫描entity包 使用别名 --> <property name="typealiasespackage" value="com.luis.entity" /> <!-- 扫描sql配置文件:mapper需要的xml文件 --> <property name="mapperlocations" value="classpath:mapper/*.xml" /> </bean> <!-- 4.配置扫描dao接口包,动态实现dao接口,注入到spring容器中 --> <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer"> <!-- 注入sqlsessionfactory --> <property name="sqlsessionfactorybeanname" value="sqlsessionfactory" /> <!-- 给出需要扫描dao接口包 --> <property name="basepackage" value="com.luis.mapper" /> </bean> </beans>
在spring中添加文spring-service.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 扫描service包下所有使用注解的类型 --> <context:component-scan base-package="com.luis.service" /> <!-- 配置事务管理器 --> <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> <!-- 注入数据库连接池 --> <property name="datasource" ref="datasource" /> </bean> <!-- 配置基于注解的声明式事务 --> <tx:annotation-driven transaction-manager="transactionmanager" /> </beans>
在spring中添加文spring-web.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- 配置springmvc --> <!-- 1.开启springmvc注解模式 --> <mvc:annotation-driven /> <!-- 2.静态资源默认servlet配置 (1)加入对静态资源的处理:js,gif,png (2)允许使用"/"做整体映射 --> <mvc:resources mapping="/resources/**" location="/resources/" /> <mvc:default-servlet-handler /> <!-- 3.定义视图解析器 --> <bean id="viewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> <property name="viewclass" value="org.springframework.web.servlet.view.jstlview" /> </bean> <!-- 4.扫描web相关的bean --> <context:component-scan base-package="com.luis.web" /> </beans>
修改web.xml文件,代码如下:
<web-app> <servlet> <servlet-name>spring-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:spring/spring-*.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>spring-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
至此,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文件。
分页已经添加依赖,可直接应用。
<dependency> <groupid>com.github.pagehelper</groupid> <artifactid>pagehelper</artifactid> <version>5.1.2</version> </dependency>
另外在spring-dao.xml文件的sqlsessionfactory的bean中添加:
<property name="plugins"> <array> <bean class="com.github.pagehelper.pageinterceptor"> <property name="properties"> <value> <!-- helperdialect:有别于3.0+版本,现在必须是helperdialect,否则spring启动加载时会报错 --> helperdialect=mysql </value> </property> </bean> </array> </property>
编写service层,userservice代码如下:
public interface userservice { user selectbyname(string name); list<user> 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<user> users = usermapper.selectbyexample(example); if (users != null && users.size() > 0) { return users.get(0); } return null; } /** * pagenum 开始页数 * pagesize 每页显示的数据条数 */ public list<user> findalluser(int pagenum, int pagesize) { //将参数传给方法实现分页 pagehelper.startpage(pagenum, pagesize); userexample example = new userexample(); list<user> 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<user> list = userservice.findalluser(1, 2); //获取分页信息 pageinfo<user> pageinfo = new pageinfo<user>(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改为:
<property name="plugins"> <array> <bean class="com.github.pagehelper.pageinterceptor"> <property name="properties"> <value> <!-- helperdialect:有别于3.0+版本,现在必须是helperdialect,否则spring启动加载时会报错 --> helperdialect=mysql </value> </property> </bean> </array> </property>
项目测试完毕!
具体的项目代码,参见于:github
本文参考了:
https://blog.csdn.net/wangyuanjun008/article/details/79121459
上一篇: PHP数组快速筛选函数的实现
下一篇: Jenkins安装与配置