spring、mybatis、mvc 整合简单例子
2015-08-04 09:02
736 查看
一、准备数据库
1、创建一张表:
1、依赖包导入:pom.xml
当然必须为MapperFactoryBean指定一个sqlSessionFactory,那就是前面我们在daoContext.xml中配置的SqlSessionFactory。
6、mvcContext.xml
由于我们使用了注解的方式声明Controller,所以我们不再需要在Spring中配置Controller的bean。首先,开启注解支持,然后知道需要扫描注解的路径
7、SqlMapConfig.xml
使用Mybatis,你需要配置一个SqlMapConfig.xml的文件,主要用于配置sql语句的映射。当然,你也可以使用注解的方式直接将sql语句写入到class中,但我个人觉得sql语句后期优化的时候可能会时常调整。如果硬编码到class中,修改不方便。
好了,下面我们来看SqlMapConfig的配置:
typeAliases部分主要是给一些java bean 配置一个别名,你使用这个bean的时候直接使用别名即可。特别在mappers配置文件中使用很方便,后面我们会讲到。这有点类似JNDI。
8、mappers部分为mybatis的Sql配置。这里你可以直接将sql配置写在mappers配置里面,也可以像本例中一样,新建一个mappers映射文件,然后在mappers
配置中指向映射文件。下面我们以FancyMapper.xml为例讲解一下mappers配置:
提示:mapper配置文件要放到 resources目录下,否则会报找不到的错误。
首先,大家注意
mybatis的增、删、改、查对应相应的insert、delete、update、select标签。注意标签的id一定要与接口中的方法名相同。
我们重点看一下查询。
注意
数据查询涉及到数据封装及将数据从ResultSet中封装为我们需要的对象。在Mybatis中,可以使用 resultType进行简单的封装。例如我们指定 resultType为Users,Mybatis会自动将查询结果封装到Users的属性中。那么如果Users关联到其他实体bean呢?这种情况下,如果我们使用 resultType的方式封装,关联对象将不会被封装!所以我们就需要另外一个更强大的东东,那就是resultMap!resultMap说白了,就是自定义数据封装方式,就是告诉mybatis哪个字段对应实体的哪个属性。
为了能使resultMap配置能够重用,通常我们会单独建一个resultMap,然后在select中使用
insert、update、delete操作方式类似,下面我们看一下insert:
parameterType属性,我们指定为Users,然后使用
好了,Mybatis配置部分就是这样。总体上讲,我个人感觉,虽然Mybatis相对Hibernate没有那么自动化,很多东西需要我们手动配置,但这也显示出Mybatis的灵活性。对后期查询优化、缓存系统建设都是有好处的。
Mybatis_中文指南
配置文件到此结束。
三、java代码:
1、model层代码: 新建一个包 com.zc.product.model ,在此包下新建java类:
2、Control层代码:
新建一个包:com.zc.product.controller ,在此包下新建java类:FancyController
mvc这个Controller执行完了需要返回到
3、Service层代码:新建java包:com.zc.product.service
这里有两个文件,一个是接口,一个是接口的实现类:
(1)接口: IFancyService
使用Mybatis+Spring后,dao层只需要这样一个接口就行了。在配置文件中作如下配置(上面已经配置过了):
Mybatis会自动创建代理类,执行接口中的方法。当然还需要数据映射器的配置
(2)实现类: FancyServiceImpl
4、dao层 新建java包 com.zc.product.dao ,新建类 IFancyMapper
四、展示效果: http://localhost:8080/zc-beauty2/product/fancy?page=0&pn=0
{"status":"ok"}
page=0pn=0
Fancy{id=1, s_type=aa, s_name=bbg, s_desc=asdfg, s_brand=brand, s_price=price, s_image=image, s_from=from, s_edittime=edittime}
完成。
1、创建一张表:
create table fancy ( id int(11) not null auto_increment, s_type varchar(255), s_name varchar(255), s_desc varchar(255), s_brand varchar(255), s_price varchar(255), s_image varchar(255), s_from varchar(255), s_edittime varchar(255), primary key(id));2、插入一条记录:
insert into fancy(s_type,s_name,s_desc,s_brand,s_price,s_image,s_from, s_edittime) values("aa", "bbg", "asdfg", "brand", "price", "image", "from", "edittime");二、配置文件
1、依赖包导入:pom.xml
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.7.RELEASE</version> <type>jar</type> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.36</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.3</version> </dependency>2、web.xml:把spring的配置文件加载进来。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!--配置Sring MVC的核心控制器DispatcherServlet --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- DispatcherServlet 默认加载的bean文件是/WEB-INF/(servlet-name)-servlet.xml 可以通过配置contextConfigLocation来改变加载的文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/conf/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!--为DispatcherServlet建立映射 --> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> </web-app>3、spring 的配置文件: applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> --> <!-- DAO配置 --> <import resource="daoContext.xml" /> <!-- IoC配置 --> <import resource="iocContext.xml" /> <!-- MVC配置 --> <import resource="mvcContext.xml" /> </beans>4、 daoContext.xml :该部分为Mybatis和Spring3的整合配置,包括数据源、SqlSessionFactory及事务管理器的配置。
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 配置数据源 --> <bean id="DataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/zc_beauty" /> <property name="username" value="root" /> <property name="password" value="123456" /> </bean> <!--Spring、MyBatis的整合,需要在 Spring 应用上下文中定义至少两样东西:一个SqlSessionFactory和至少一个数据映射器类(UserMapper->iocContext.xml)。--> <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="/WEB-INF/conf/SqlMapConfig.xml" /> <property name="dataSource" ref="DataSource" /> </bean> <!-- 配置事务管理器 <bean id="TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="DataSource" /> </bean>--> </beans>5、iocContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 数据映射器类 mapper bean --> <bean id="FancyMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" > <property name="sqlSessionFactory" ref="SqlSessionFactory" /> <!-- 注意指定的映射器类必须是一个接口,而不是具体的实现类 --> <property name="mapperInterface" value="com.zc.product.dao.IFancyMapper" /> </bean> </beans>这部分是Dao层bean的配置。注意,这里bean的class并非直接指向dao层的类,而是指向org.mybatis.spring.mapper.MapperFactoryBean,并将dao层接口IUserMapper作为MapperFactoryBean的mapperInterface属性。mybatis会自动帮我们创建一个代理类,执行IUserMapper里面的方法。也就是说,只要我们写好了mybatis的SqlMapConfig.xml,dao层只需要一个接口Mapper就行了,甚至连实现类都不要,因为myBatis自动帮我们完成。强大吧!
当然必须为MapperFactoryBean指定一个sqlSessionFactory,那就是前面我们在daoContext.xml中配置的SqlSessionFactory。
6、mvcContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" 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-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 参考资料:http://code.google.com/p/bounding/wiki/SpringMVC3 --> <!-- 国际化配置 参考:http://hi.baidu.com/sonmeika/blog/item/8069b2dd7db1c9395882dd29.html <bean id="localeResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver"> </bean> --> <!-- 注解支持 --> <mvc:annotation-driven /> <!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 --> <context:component-scan base-package="com.zc.product.controller" /> <context:component-scan base-package="com.zc.product.service" /> <!-- 允许定义过滤器将基包下的某些类纳入或排除 <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> --> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 <bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> --> <!--配置视图解析器--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass"> <value>org.springframework.web.servlet.view.InternalResourceView</value> </property> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp"/> <!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 --> </bean> </beans>这部分是Spring mvc的配置。viewResolver视图解析器是必须的。其属性prefix表示某个视图指向到那个路径,suffix属性表示视图的后缀(如果配置为jsp则表示该视图使用jsp页面进行渲染)。例如某个Controller返回一个login视图,根据当前的配置,表示该视图使用
/WEB-INF/page/login.jsp进行渲染。这一点跟struts2有点像.
由于我们使用了注解的方式声明Controller,所以我们不再需要在Spring中配置Controller的bean。首先,开启注解支持,然后知道需要扫描注解的路径
7、SqlMapConfig.xml
使用Mybatis,你需要配置一个SqlMapConfig.xml的文件,主要用于配置sql语句的映射。当然,你也可以使用注解的方式直接将sql语句写入到class中,但我个人觉得sql语句后期优化的时候可能会时常调整。如果硬编码到class中,修改不方便。
好了,下面我们来看SqlMapConfig的配置:
<?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> 这个配置使全局的映射器启用或禁用缓存 <setting name="cacheEnabled" value="true" /> 允许 JDBC 支持生成的键。需要适合的驱动。如果设置为 true 则这个设置强制生成的键被使用,尽管一些驱动拒绝兼容但仍然有效(比如 Derby) <setting name="useGeneratedKeys" value="true" /> 配置默认的执行器。SIMPLE 执行器没有什么特别之处。REUSE 执行器重用预处理语句。BATCH 执行器重用语句和批量更新 <setting name="defaultExecutorType" value="REUSE" /> 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 <setting name="lazyLoadingEnabled" value="true"/> 设置超时时间,它决定驱动等待一个数据库响应的时间。 <setting name="defaultStatementTimeout" value="25000"/> </settings>--> <!-- 别名配置 --> <typeAliases> <typeAlias alias="Fancy" type="com.zc.product.model.Fancy" /> </typeAliases> <!-- 指定映射器路径 --> <!-- <mappers> <mapper resource="/WEB-INF/conf/mapper/FancyMapper.xml" /> </mappers>--> <mappers> <mapper resource="mappers/FancyMapper.xml" /> </mappers> </configuration>settings部分都是可选配置,如果 你不配置,mybatis都设有默认值。附件指南中对相关配置讲解的很详细。
typeAliases部分主要是给一些java bean 配置一个别名,你使用这个bean的时候直接使用别名即可。特别在mappers配置文件中使用很方便,后面我们会讲到。这有点类似JNDI。
8、mappers部分为mybatis的Sql配置。这里你可以直接将sql配置写在mappers配置里面,也可以像本例中一样,新建一个mappers映射文件,然后在mappers
配置中指向映射文件。下面我们以FancyMapper.xml为例讲解一下mappers配置:
提示:mapper配置文件要放到 resources目录下,否则会报找不到的错误。
<?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"> <!-- 这里namespace必须是接口的路径,不然要运行的时候要报错 “is not known to the MapperRegistry”--> <mapper namespace="com.zc.product.dao.IFancyMapper"> <!-- 这里的id必须和接口中的方法名相同,不然运行的时候也要报错 --> <select id="showFancyPagePn" resultMap="showFancyPagePnMap"> <!-- 这里sql结尾不能加分号,否则报“ORA-00911”的错误 --> select * from fancy </select> <!-- 自定义映射关系 --> <resultMap id="showFancyPagePnMap" type="Fancy"> <!--Fancy 是com.zc.model.Fancy的别名 --> <!-- 标记结果作为ID 可以帮助提高整体效能 --> <id property="id" column="id"/> <!-- 注入到字段或JavaBean 属性的普通结果 --> <result property="s_type" column="s_type" /> <result property="s_name" column="s_name" /> <result property="s_desc" column="s_desc" /> <result property="s_brand" column="s_brand" /> <result property="s_price" column="s_price" /> <result property="s_image" column="s_image" /> <result property="s_from" column="s_from" /> <result property="s_edittime" column="s_edittime" /> </resultMap> </mapper>
首先,大家注意
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">我这里使用的是mybatis3的配置文件格式,如果使用较低版本的ibatis,标签会有一些差异!mapper标签有个namespace,指向dao层的接口(这里,顺便说一下,我们使用Spring3+mybatis+mysql整合,dao层只需要一个接口就行了,不再需要实现类。mybatis会自动创建代理类完成数据查询及封装,这个查询条件及封装依据就取决于我们这里的mappers映射文件。另外,如果特殊的操作,比如数据库备份之类,你可以手动创建实现类完成操作,附件里面有实例。)
mybatis的增、删、改、查对应相应的insert、delete、update、select标签。注意标签的id一定要与接口中的方法名相同。
我们重点看一下查询。
<select id="findAllUsers" resultType="Users" useCache="true"> select * from users </select>
注意
resultType="Users"中的Users即前面我们配置的别名,否则要引用
com.hl.usersmanager.model.Users。如果有参数,则需要指定参数类型:
<select id="findUserByName" resultType="Users" parameterType="java.lang.String"> select * from users where name=#{name} </select>
数据查询涉及到数据封装及将数据从ResultSet中封装为我们需要的对象。在Mybatis中,可以使用 resultType进行简单的封装。例如我们指定 resultType为Users,Mybatis会自动将查询结果封装到Users的属性中。那么如果Users关联到其他实体bean呢?这种情况下,如果我们使用 resultType的方式封装,关联对象将不会被封装!所以我们就需要另外一个更强大的东东,那就是resultMap!resultMap说白了,就是自定义数据封装方式,就是告诉mybatis哪个字段对应实体的哪个属性。
为了能使resultMap配置能够重用,通常我们会单独建一个resultMap,然后在select中使用
resultMap=" resultMapId"的方式引用。在resultMap中,如果是一一映射,便使用association(注意其javaType属性,就是告诉Mybatis这个关联要封装为什么java bean);如果是一对多则使用collection(需要在实体bean中指定关联对象为List集合,例如
private List<LoginLog> loginLogs;)
insert、update、delete操作方式类似,下面我们看一下insert:
<insert id="insertUser" parameterType="Users"> <!-- 这里sql结尾不能加分号,否则报“ORA-00911”的错误 --> insert into users(name,age,phone) values(#{name},#{age},#{phone}) </insert>
parameterType属性,我们指定为Users,然后使用
values(#{name},#{age},#{phone}的方式,Mybatis会自动将Users的name,age,phone属性值作为参数。
好了,Mybatis配置部分就是这样。总体上讲,我个人感觉,虽然Mybatis相对Hibernate没有那么自动化,很多东西需要我们手动配置,但这也显示出Mybatis的灵活性。对后期查询优化、缓存系统建设都是有好处的。
Mybatis_中文指南
配置文件到此结束。
三、java代码:
1、model层代码: 新建一个包 com.zc.product.model ,在此包下新建java类:
package com.zc.product.model; public class Fancy { private long id; private String s_type; private String s_name; private String s_desc; private String s_brand; private String s_price; private String s_image; private String s_from; private String s_edittime; }这个类和上面建的表fancy对应。注意最后加上get、set方法。
2、Control层代码:
新建一个包:com.zc.product.controller ,在此包下新建java类:FancyController
package com.zc.product.controller; import com.zc.product.model.Fancy; import com.zc.product.service.IFancyService; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/product") public class FancyController { // 使用注解实现自动装配 不需要再写get set方法以及在context中配置bean @Autowired private IFancyService fancyService; @RequestMapping(value = "/test") @ResponseBody public String test() { return "{\"status\":\"ok\"}"; } @RequestMapping(value = "/fancy") @ResponseBody public String showFancyPagePn(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "pn", required = false) Integer pn) { String pageStr = new String(); pageStr += ("page="+page+"pn="+pn+"<br />"); List<Fancy> fancyList = null; fancyList = fancyService.showFancyPagePn(page, pn); for(Fancy fancy : fancyList) { pageStr += (fancy.toString()+"<br />"); } return "{\"status\":\"ok\"}<br />" + pageStr; } }在较低版本Spring中,Controller需要实现一个接口,在Spring3中,我们采用注解的方式实现。只需要加入一个
@Controller注解即可(当然,你需要在配置文件中开启注解功能。再使用注解声明一个
IUserService(Service层接口)属性。
@RequestMapping(value = "findUserByName.do")表示拦截
findUserByName.do的请求,交由被注解的方法处理。
findUserByName方法有两个参数,第一个参数
name从页面上传入。由于我们需要找到
Users后将其
userPhone属性返回给页面,所以我们需要第二个参数
model。这个参数由Spring自行封装,我们不管它。然后将
users.getPhone()放入
model中。就好像我们往
response中放参数一样。最后返回一个
String类型的
showUser,告诉Spring
mvc这个Controller执行完了需要返回到
showUser这个视图。具体由哪个页面进行渲染,由Spring mvc的视图解析器配置决定。当然
findUserByName也可以使用其他返回类型。这里有篇帖子讲得比较详细,分享给大家:http://www.ibm.com/developerworks/cn/java/j-lo-spring25-mvc/
3、Service层代码:新建java包:com.zc.product.service
这里有两个文件,一个是接口,一个是接口的实现类:
(1)接口: IFancyService
package com.zc.product.service; import com.zc.product.model.Fancy; import java.util.List; public interface IFancyService { public List<Fancy> showFancyPagePn(int page, int pn); }
使用Mybatis+Spring后,dao层只需要这样一个接口就行了。在配置文件中作如下配置(上面已经配置过了):
<!-- 数据映射器类 mapper bean --> <bean id="UserMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="sqlSessionFactory" ref="SqlSessionFactory" /> <!-- 注意指定的映射器类必须是一个接口,而不是具体的实现类 --> <property name="mapperInterface" value="com.hl.usersmanager.dao.IUserMapper" /> </bean>
Mybatis会自动创建代理类,执行接口中的方法。当然还需要数据映射器的配置
(2)实现类: FancyServiceImpl
package com.zc.product.service; import com.zc.product.dao.IFancyMapper; import com.zc.product.model.Fancy; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class FancyServiceImpl implements IFancyService{ @Autowired private IFancyMapper fancyMapper; @Override public List<Fancy> showFancyPagePn(int page, int pn) { return fancyMapper.showFancyPagePn(page, pn); } }在Service层 IFancyMapper 的实现类上加入
@Service注解。
FancyController中就会自动装载这个bean。
4、dao层 新建java包 com.zc.product.dao ,新建类 IFancyMapper
package com.zc.product.dao; import com.zc.product.model.Fancy; import java.util.List; public interface IFancyMapper { public List<Fancy> showFancyPagePn(int page, int pn); }这个接口是和 mapper 文件相对应的,这里只要定义这个接口就可以了,不用定义实现类,mybatis帮我们实现了。
四、展示效果: http://localhost:8080/zc-beauty2/product/fancy?page=0&pn=0
{"status":"ok"}
page=0pn=0
Fancy{id=1, s_type=aa, s_name=bbg, s_desc=asdfg, s_brand=brand, s_price=price, s_image=image, s_from=from, s_edittime=edittime}
完成。
相关文章推荐
- JAVA实现FTP
- Java对象的创建过程
- JAVA基础一大堆0804XML+UDP
- java 基础
- JDK和JRE的区别
- Java之集合中泛型的使用【入门篇】
- 【转】derby+hibernate+spring
- 重学java23种设计模式(11)享元模式
- 详解三种java实现多线程的方式
- Java - IntelliJ IDEA 编辑注释模板
- java String 中的==null、length()==0、=="" 理解
- java.net.ServerSocket详解
- java 定时器
- 第一个Struts2应用
- Spring Boot 让 MyBatis 打印日志的方法
- java.net.URLConnection详解
- 改换jdk版本可能出现的问题
- java基础知识
- 设置单个项目的jdk版本
- JAVA中转义字符: