1、pom.xml的jar包依赖配置
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yzh</groupId>
<artifactId>hBSpringMvcTestPro</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>hBSpringMvcTestPro Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<!-- 自定义jar包版本动态注入 ,标签如下:并使用占位符${junit.version}来传值-->
<springs.version>4.3.2.RELEASE</springs.version>
<log4j.core.version>2.6.2</log4j.core.version>
<log4j.slf4j.impl.version>2.6.2</log4j.slf4j.impl.version>
<jstl.version>1.2</jstl.version>
<hibernate.validator.version>5.3.0.Alpha1</hibernate.validator.version>
<jboss.logging.version>3.3.0.Final</jboss.logging.version>
<validation.api.version>1.1.0.Final</validation.api.version>
<commons.io.version>2.4</commons.io.version>
<commons.fileupload.version>1.3</commons.fileupload.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- mybatis依赖jar包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<!-- spring框架测试jar包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${springs.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${springs.version}</version>
</dependency>
<!-- 写日志的jar包依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>commons-log</artifactId>
<version>1.0.0.RELEASE</version>
<scope>test</scope>
</dependency>
<!--hibernate核心包-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.11.Final</version>
</dependency>
<!--JPA包支持
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.11.Final</version>
</dependency>
<!-- spring data支持 start -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
<!--end! -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${springs.version}</version>
</dependency>
<!-- oracle数据库连接jar包 >-->
<dependency>
<groupId>com.hynnet</groupId>
<artifactId>oracle-driver-ojdbc6</artifactId>
<version>12.1.0.1</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
<scope>test</scope>
</dependency>
<!-- spring框架上下文解析jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springs.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
<scope>compile</scope>
</dependency>
<!-- spring与mybatis的适配包,用于在spring中整合mybatis框架用 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
<scope>compile</scope>
</dependency>
<!-- spring框架事务注解jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${springs.version}</version>
<!-- <scope>runtime</scope> -->
<scope>compile</scope>
</dependency>
<!-- spring框架数据库连接jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${springs.version}</version>
<scope>compile</scope>
</dependency>
<!-- 持久化框架支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${springs.version}</version>
</dependency>
<!-- 数据源框架包 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<!--日志处理核心(core)包 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.core.version}</version>
<scope>compile</scope>
</dependency>
<!-- 日志打印信息 jar包依赖,没有该jar,将无法打印出日志信息 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.slf4j.impl.version}</version>
</dependency>
<!-- springmvc框架jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springs.version}</version>
<scope>compile</scope>
</dependency>
<!-- jstl依赖jar包 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20151123</version>
</dependency>
<!-- Hibernate的校验框架 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate.validator.version}</version>
</dependency>
<!-- jboss日志框架 -->
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>${jboss.logging.version}</version>
</dependency>
<!-- 校验器依赖jar包 -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>${validation.api.version}</version>
</dependency>
<!-- 上传图片jar包 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons.io.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons.fileupload.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>struts</groupId>
<artifactId>struts</artifactId>
<version>1.2.9</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.2.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.2.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!--分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.2.1</version>
<classifier>jdk15</classifier>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- fastjson工具依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
</dependencies>
<build>
<finalName>hBSpringMvcTestPro</finalName>
</build>
</project>
2、配置数据源资源文件oracle.properties
myjdbc.driver=oracle.jdbc.driver.OracleDriver
myjdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
myjdbc.username:test
myjdbc.password:a
myjdbc.initialSize=10
myjdbc.maxTotal=100
myjdbc.minIdle=5
myjdbc.maxWait=-1
3、配置hibernate SQL打印、数据库方言、自动建表等资源信息
#hibernate config
hibernate.dialect:org.hibernate.dialect.OracleDialect
hibernate.show_sql:true
hibernate.format_sql:true
hibernate.hbm2ddl.auto:update
4、配置spring-hibernate.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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
default-lazy-init="true">
<!--1.开启注解-->
<context:annotation-config/>
<!-- 启动组件扫描,排除@Controller组件,该组件由SpringMVC配置文件扫描 -->
<!--2.注解激活器 -->
<context:component-scan base-package="com.yzh.maven.*">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 加载oracle、hibernate属性配置文件,中间以逗号隔开 -->
<context:property-placeholder location="classpath:properties/oracle.properties,classpath:properties/hibernate.properties" />
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<!--注意属性是固定的,不能自定义-->
<property name="driverClassName" value="${myjdbc.driver}"/>
<property name="url" value="${myjdbc.url}"/>
<property name="username" value="${myjdbc.username}"/>
<property name="password" value="${myjdbc.password}"/>
<property name="maxWaitMillis" value="${myjdbc.maxWait}"/>
</bean>
<!-- JPA实体管理器工厂 -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
<!-- 加入定制化包路径 -->
<property name="packagesToScan" value="com.yzh.maven.entity" />
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <!--hibernate根据实体自动生成数据库表-->
<prop key="hibernate.dialect">${hibernate.dialect}</prop> <!--指定数据库方言-->
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <!--在控制台显示执行的数据库操作语句-->
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <!--在控制台显示执行的数据哭操作语句(格式)-->
</props>
</property>
</bean>
<!-- 设置JPA实现厂商的特定属性 -->
<bean id="hibernateJpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="${hibernate.dialect}"/>
</bean>
<!-- Jpa 事务配置 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!-- Spring Data Jpa配置 -->
<jpa:repositories base-package="com.yzh.maven" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/>
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
</beans>
5、配置springmvc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!--以下映射器和适配器的配置可以通过注解驱动来代替 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- SpringMVC的扫描范围 -->
<context:component-scan base-package="com.yzh.maven.controller" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!--对静态资源文件的访问 必须要设置,因为在springmvc的配置中配置了/匹配所有请求,
此工程所有的请求(.do ,addUser,js/image/css)都会被springmvc解析,
必须对所有的静态资源文件进行过滤放行 -->
<mvc:resources mapping="/resource/**" location="/resource/" />
<!-- 拦截器定义 -->
<mvc:interceptors>
<mvc:interceptor>
<!-- 个人中心也需要登陆 以admin开头的配置都会进行拦截-->
<mvc:mapping path="/admin/**"></mvc:mapping>
<!-- 这个是设置不会进入拦截器的路径 -->
<mvc:exclude-mapping path="/resource/**"/>
<!-- 拦截器进入的类,返回false表示不会进入输入的路径 -->
<bean class="com.yzh.maven.interceptor.HandlerInterceptor1" />
</mvc:interceptor>
</mvc:interceptors>
<!-- 视图渲染 jsp/freemaker/velocity-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 文件的后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
6、配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>hBSpringMvcTestPro</display-name>
<!-- 加载Spring IOC容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-hibernate.xml</param-value>
</context-param>
<!-- spring上下文监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Introspector缓存清除监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>yzh_ssh</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置springMVC的配置文件 -->
<!-- 如果不配置下面选项,系统默认加载classpath下面名为[servlet-name]-servlet.xml的文件 springmvc01-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<!-- 过滤controller层请求,只能通过*.action请求 -->
<servlet-mapping>
<servlet-name>yzh_ssh</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
7、编写实体类
package com.yzh.maven.entity;
import java.io.Serializable;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import com.yzh.maven.base.entity.Entity;
@javax.persistence.Entity
@Table(name="userinfo",schema="TEST")
public class UserInfo extends Entity<UserInfo> implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int userId;
private String userName;
private String password;
private int age;
private String sex;
private String tel;
private String email;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public UserInfo(String userName, String password, int age, String sex,
String tel, String email) {
super();
this.userName = userName;
this.password = password;
this.age = age;
this.sex = sex;
this.tel = tel;
this.email = email;
}
public UserInfo() {
super();
}
@Override
public int compareTo(UserInfo u) {
if(this == u){
return 0;
}else{
if(userName.compareTo(u.getUserName()) > 0){
return 1;
}else if(userName.compareTo(u.getUserName()) == 0){
return 0;
}else{
return -1;
}
}
}
@Override
public String toString() {
return "UserInfo [userName=" + userName + ", password=" + password
+ ", age=" + age + ", sex=" + sex + ", tel=" + tel + ", email="
+ email + "]";
}
}
说明:这里使用了hibernate的JPA注解,用于自动建表,当tomcat启动时,hibernate通过这些注解会自动帮我们在数据库中建立表格,那么为什么可以在启动服务器时自动表呢?不是还要在hibernate.cfg.xml中注册实体类吗?实际上hibernate.cfg.xml的功能由spring-hibernate.xml接管了:
<!--配置session工厂-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 设置Spring去哪个包查找相应的实体类,使用注解创建表时使用 -->
<property name="packagesToScan" value="com.yzh.maven.entity"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <!--hibernate根据实体自动生成数据库表-->
<prop key="hibernate.dialect">${hibernate.dialect}</prop> <!--指定数据库方言-->
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <!--在控制台显示执行的数据库操作语句-->
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <!--在控制台显示执行的数据哭操作语句(格式)-->
</props>
</property>
</bean>
如果时spring-data的JPA,则可以这样来配置:
<!-- JPA实体管理器工厂 -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
<!-- 加入定制化包路径 -->
<property name="packagesToScan" value="com.yzh.maven.entity" />
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <!--hibernate根据实体自动生成数据库表-->
<prop key="hibernate.dialect">${hibernate.dialect}</prop> <!--指定数据库方言-->
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <!--在控制台显示执行的数据库操作语句-->
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <!--在控制台显示执行的数据哭操作语句(格式)-->
</props>
</property>
</bean>
<!-- 设置JPA实现厂商的特定属性 -->
<bean id="hibernateJpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="${hibernate.dialect}"/>
</bean>
<!-- Jpa 事务配置 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!-- Spring Data Jpa配置 -->
<jpa:repositories base-package="com.yzh.maven" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/>
8、编写dao接口
package com.yzh.maven.mapper;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import com.yzh.maven.entity.UserInfo;
/**
*
* @author
*/
public interface UserInfoDao extends JpaRepository<UserInfo,Integer>{
/**
* 查询所有
* @return
*/
@Query("select u from UserInfo u")
public List<UserInfo> findAll();
}
说明:如果你需要使用@Query注解,那么你必须继承JpaRepository<UserInfo,Integer>接口,这样该接口就能支持@Query注解的HQL了(该注解还支持普通sql语句),需要注意的是,@Query注解是写在Dao层的方法上,而不是service接口或是类的方法上!
9、编写ServiceImpl实现类
package com.yzh.maven.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yzh.maven.abstracts.EntityAbstract;
import com.yzh.maven.entity.UserInfo;
import com.yzh.maven.mapper.UserInfoDao;
import com.yzh.maven.service.UserInfoService;
@Service
public class UserInfoServiceImpl extends EntityAbstract<UserInfo> implements UserInfoService{
@Autowired
public UserInfoDao userInfoDao;
@Override
public void addUser(UserInfo userInfo) {
}
@Override
public void removeUser(String id) {
}
@Override
public void saveOrUpdateUser(UserInfo User) {
}
@Override
public UserInfo findById(String id) {
return null;
}
@Override
public UserInfo findByUsername(String username) {
return null;
}
@Override
public List<UserInfo> findAll() {
return userInfoDao.findAll();
}
}
10、编写service接口
package com.yzh.maven.service;
import java.util.List;
import com.yzh.maven.base.plugin.Pagination;
import com.yzh.maven.entity.UserInfo;
/**
*
* @author
*/
public interface UserInfoService{
/**
* 添加
* @param User
*/
public void addUser(UserInfo User);
/**
* 删除
* @param id
*/
public void removeUser(String id);
/**
* 保存或修改
* @param User
*/
public void saveOrUpdateUser(UserInfo User);
/**
* 根据id查询单个
* @param id
* @return
*/
public UserInfo findById(String id);
/**
* 根据用户名查询
* @param id
* @return
*/
public UserInfo findByUsername(String username);
/**
* 查询所有
* @return
*/
public List<UserInfo> findAll();
/**
* 分页查询
* @return
*/
public void queryPage(Pagination<UserInfo> pagination);
}
11、测试
package com.yzh.maven.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yzh.maven.base.model.DataMessage;
import com.yzh.maven.base.plugin.Pagination;
import com.yzh.maven.entity.UserInfo;
import com.yzh.maven.service.UserInfoService;
/**
* @FileName UserInfoController.java
* @Description: 用户信息操作Controller
* @Date 2018年8月31日
* @author yzh
* @version 1.0.3
*
*/
@Controller
@RequestMapping("/list")
public class UserInfoController{
@Autowired
private UserInfoService userInfoService;
private static final Logger LOG = LoggerFactory.getLogger(UserInfoController.class);
@ResponseBody
@RequestMapping(value = "/listUserInfos.action")
public DataMessage listUserInfos(Pagination<UserInfo> pageParams) throws Exception {
userInfoService.queryPage(pageParams);
return DataMessage.successData(pageParams);
}
@GetMapping(value = "/toIndex.action")
public String toIndex(){
return "/index";
}
}