1.二级缓存:
需要导入二级缓存jar包:
mybatis03:
ehcache.xml:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> <diskStore path="java.io.tmpdir"/> <!-- Mandatory Default Cache configuration. These settings will be applied to caches created programmtically using CacheManager.add(String cacheName) --> <!-- name:缓存名称。 maxElementsInMemory:缓存最大个数。 eternal:对象是否永久有效,一但设置了,timeout将不起作用。 timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。 timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。 overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。 diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。 maxElementsOnDisk:硬盘最大缓存个数。 diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false. diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。 memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。 clearOnFlush:内存数量最大时是否清除。 --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> </ehcache>
TbUserMapper.xml:
<?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="cn.zzsxt.mapper.TbUserMapper"> <!-- 开启二级缓存 --> <!-- <cache></cache> --> <!--设置映射文件中cache标签的type值为ehcache的实现类 --> <cache type="org.mybatis.caches.ehcache.EhcacheCache"></cache> <select id="findUserById" parameterType="java.lang.Long" resultType="cn.zzsxt.entity.TbUser"> select * from tb_user where id=#{id} </select> </mapper>
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> <!-- 加载属性文件 --> <properties resource="db.properties"/> <!-- 全局参数的设置 --> <settings> <!--延迟加载 默认false --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 积极加载 默认true --> <setting name="aggressiveLazyLoading" value="false"/> <!-- 开启二级缓存 --> <setting name="cacheEnabled" value="true"/> </settings> <environments default="development"> <environment id="development"> <!-- 使用jdbc中的事务 --> <transactionManager type="JDBC"></transactionManager> <!-- 配置数据源 --> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <!-- 引入映射文件 --> <mappers> <!-- <mapper resource="cn/zzsxt/mapper/TbUserMapper.xml"/> --> <!-- 注意:此种方法要求mapper接口和mapper映射文件要名称相同,且放到同一个目录下; --> <!-- <mapper class="cn.zzsxt.mapper.TbUserMapper"/> --> <!-- 注意:此种方法要求mapper接口和mapper映射文件要名称相同,且放到同一个目录下; --> <package name="cn.zzsxt.mapper"/> </mappers> </configuration>
2.ssI(Ibatis)DaoDemo集成:
jar:
aopalliance.jar
asm-4.2.jar
aspectjweaver.jar
c3p0-0.9.5.2.jar
cglib-3.1.jar
commons-logging-1.1.3.jar
commons-logging-1.2.jar
ehcache-2.10.3.jar
log4j-1.2.17.jar
log4j-api-2.2.jar
log4j-core-2.2.jar
mchange-commons-java-0.2.11.jar
mybatis-3.3.0.jar
mybatis-ehcache-1.0.3.jar
mybatis-spring-1.2.5.jar
mysql-connector-java-5.1.40.jar
slf4j-api-1.7.12.jar
slf4j-api-1.7.7.jar
slf4j-log4j12-1.7.12.jar
spring-aop-4.3.9.RELEASE.jar
spring-aspects-4.3.9.RELEASE.jar
spring-beans-4.3.9.RELEASE.jar
spring-context-4.3.9.RELEASE.jar
spring-context-support-4.3.9.RELEASE.jar
spring-core-4.3.9.RELEASE.jar
spring-expression-4.3.9.RELEASE.jar
spring-instrument-4.3.9.RELEASE.jar
spring-instrument-tomcat-4.3.9.RELEASE.jar
spring-jdbc-4.3.9.RELEASE.jar
spring-jms-4.3.9.RELEASE.jar
spring-messaging-4.3.9.RELEASE.jar
spring-orm-4.3.9.RELEASE.jar
spring-oxm-4.3.9.RELEASE.jar
spring-test-4.3.9.RELEASE.jar
spring-tx-4.3.9.RELEASE.jar
spring-web-4.3.9.RELEASE.jar
spring-webmvc-4.3.9.RELEASE.jar
spring-webmvc-portlet-4.3.9.RELEASE.jar
spring-websocket-4.3.9.RELEASE.jar
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: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-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 注解扫描!!!--> <context:component-scan base-package="cn.zzsxt.ssm"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> <mvc:annotation-driven></mvc:annotation-driven> <mvc:default-servlet-handler/> </beans>
jdbc.properties:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssh
username=root
password=root
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-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 1.加载properties配置文件 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties"></property> </bean> <!-- 2.配置数据源DataSource --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${driver}"></property> <property name="jdbcUrl" value="${url}"></property> <property name="user" value="${username}"></property> <property name="password" value="${password}"></property> </bean> <!-- 3.配置sqlSessionFactory,并注入数据源 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="mapperLocations"> <list> <value>classpath:cn/zzsxt/ssm/entity/Userinfo.xml</value> </list> </property> </bean> <!-- 4.配置SqlSessionTemplate,注入sqlSessionFactory --> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg> </bean> <!-- 5.配置DAO --> <bean id="userinfoDao" class="cn.zzsxt.ssm.dao.impl.UserinfoDaoImpl"> <property name="sqlSessionTemplate" ref="sqlSessionTemplate"></property> </bean> <!-- 6.配置Service --> <bean id="userinfoService" class="cn.zzsxt.ssm.service.impl.UserinfoServiceImpl"> <property name="userinfoDao" ref="userinfoDao"></property> </bean> <!-- 配置声明式事务 --> <!-- 配置事务管理器,并注入数据源 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="*" propagation="SUPPORTS"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* cn.zzsxt.ssm.service.*.*(..))" id="serviceMethods"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/> </aop:config> </beans>
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml</param-value> </context-param> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
UserinfoController.java:
package cn.zzsxt.ssm.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import cn.zzsxt.ssm.entity.Userinfo; import cn.zzsxt.ssm.service.UserinfoService; @Controller @RequestMapping("/user") public class UserinfoController { @Autowired private UserinfoService userinfoService; public void setUserinfoService(UserinfoService userinfoService) { this.userinfoService = userinfoService; } /** * 查询用户信息 * @param model * @return */ @RequestMapping("/list") public String list(Model model){ List<Userinfo> list = userinfoService.findAllUsers(); model.addAttribute("list", list); return "list"; } /** * 跳转WEB-INF/jsp/add.jsp * @return */ @RequestMapping(value="/add",method=RequestMethod.GET) public String add(){ return "add"; } /** * 添加用户 * @param user * @return */ @RequestMapping(value="/add",method=RequestMethod.POST) public String add(Userinfo user){ int count = userinfoService.addUser(user); if(count>0){ return "redirect:/user/list"; } return "error"; } }
UserinfoDaoImpl.java:
package cn.zzsxt.ssm.dao.impl; import java.util.List; import org.mybatis.spring.SqlSessionTemplate; import cn.zzsxt.ssm.dao.UserinfoDao; import cn.zzsxt.ssm.entity.Userinfo; public class UserinfoDaoImpl implements UserinfoDao { private SqlSessionTemplate sqlSessionTemplate; public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) { this.sqlSessionTemplate = sqlSessionTemplate; } @Override public List<Userinfo> findAllUsers() { return sqlSessionTemplate.selectList("user.findAllUsers"); } @Override public int addUser(Userinfo user) { return sqlSessionTemplate.insert("user.addUser",user); } }
Userinfo.xml:
<?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="user"> <select id="findAllUsers" resultType="cn.zzsxt.ssm.entity.Userinfo"> select * from userinfo </select> <insert id="addUser" parameterType="cn.zzsxt.ssm.entity.Userinfo"> insert into userinfo(userName,userPass,userType) values(#{userName},#{userPass},#{userType}) </insert> </mapper>
UserinfoService.java:
package cn.zzsxt.ssm.service.impl; import java.util.List; import cn.zzsxt.ssm.dao.UserinfoDao; import cn.zzsxt.ssm.entity.Userinfo; import cn.zzsxt.ssm.service.UserinfoService; public class UserinfoServiceImpl implements UserinfoService { private UserinfoDao userinfoDao; public void setUserinfoDao(UserinfoDao userinfoDao) { this.userinfoDao = userinfoDao; } @Override public List<Userinfo> findAllUsers() { return userinfoDao.findAllUsers(); } @Override public int addUser(Userinfo user) { return userinfoDao.addUser(user); } }
Test:
package cn.zzsxt.ssm.test; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.zzsxt.ssm.entity.Userinfo; import cn.zzsxt.ssm.service.UserinfoService; public class Test { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); UserinfoService userinfoService = ac.getBean("userinfoService",UserinfoService.class); List<Userinfo> list = userinfoService.findAllUsers(); for (Userinfo userinfo : list) { System.out.println(userinfo); } } }
3.SSM集成:
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <!-- ContextLoaderListener解析spring的配置文件 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml</param-value> </context-param> <!-- 解决post提交乱码问题 --> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- spring mvc的前端控制器,类似struts2的核心过滤器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 通过contextConfigLocation参数指定配置文件的位置,默认在WEB-INF/查找名称为 [servlet-name]-servlet.xml --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!-- servlet随web容器而启动 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
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: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-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 注解扫描!!!--> <context:component-scan base-package="cn.zzsxt.ssm"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> <mvc:annotation-driven></mvc:annotation-driven> <mvc:default-servlet-handler/> </beans>
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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 扫描注解包 --> <context:component-scan base-package="cn.zzsxt.ssm"></context:component-scan> <!-- 2.配置数据源DataSource --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> </bean> <!-- 3.配置sqlSessionFactory,并注入数据源 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 加载cn/zzsxt/ssm/mapper/所有以 Mapper.xml结尾的映射文件 --> <property name="mapperLocations" value="classpath:cn/zzsxt/ssm/mapper/*Mapper.xml"></property> </bean> <!-- 4.配置MapperScannerConfigurer,用于扫描指定包下的Mapper接口,生成代理对象的id为Mapper接口名第一个字母小写 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.zzsxt.ssm.mapper"></property> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> <!-- 配置声明式事务 --> <!-- 配置事务管理器,并注入数据源 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="*" propagation="SUPPORTS"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* cn.zzsxt.ssm.service.*.*(..))" id="serviceMethods"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/> </aop:config> </beans>
UserinfoController.java:
package cn.zzsxt.ssm.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import cn.zzsxt.ssm.entity.Userinfo; import cn.zzsxt.ssm.service.UserinfoService; @Controller @RequestMapping("/user") public class UserinfoController { @Autowired private UserinfoService userinfoService; /** * 查询用户信息 * @param model * @return */ @RequestMapping("/list") public String list(Model model){ List<Userinfo> list = userinfoService.findAllUsers(); model.addAttribute("list", list); return "list"; } /** * 跳转WEB-INF/jsp/add.jsp * @return */ @RequestMapping(value="/add",method=RequestMethod.GET) public String add(){ return "add"; } /** * 添加用户 * @param user * @return */ @RequestMapping(value="/add",method=RequestMethod.POST) public String add(Userinfo user){ int count = userinfoService.addUser(user); if(count>0){ return "redirect:/user/list"; } return "error"; } }
Userinfo.java:
package cn.zzsxt.ssm.entity; import java.io.Serializable; public class Userinfo implements Serializable { private int userId; private String userName; private String userPass; private int userType; public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPass() { return userPass; } public void setUserPass(String userPass) { this.userPass = userPass; } public int getUserType() { return userType; } public void setUserType(int userType) { this.userType = userType; } @Override public String toString() { return "Userinfo [userId=" + userId + ", userName=" + userName + ", userPass=" + userPass + ", userType=" + userType + "]"; } }
UserinfoMapper.java:
package cn.zzsxt.ssm.mapper; import java.util.List; import cn.zzsxt.ssm.entity.Userinfo; public interface UserinfoMapper { /** * 查询用户信息 * @return */ List<Userinfo> findAllUsers(); /** * 新增用户 * @param user * @return */ int addUser(Userinfo user); }
UserinfoMapper.xml:
<?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="cn.zzsxt.ssm.mapper.UserinfoMapper"> <select id="findAllUsers" resultType="cn.zzsxt.ssm.entity.Userinfo"> select * from userinfo </select> <insert id="addUser" parameterType="cn.zzsxt.ssm.entity.Userinfo"> insert into userinfo(userName,userPass,userType) values(#{userName},#{userPass},#{userType}) </insert> </mapper>
UserinfoServiceImpl.java:
package cn.zzsxt.ssm.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import cn.zzsxt.ssm.entity.Userinfo; import cn.zzsxt.ssm.mapper.UserinfoMapper; import cn.zzsxt.ssm.service.UserinfoService; //<bean id="userinfoService" class="cn.zzsxt.ssm.service.impl.UserinfoServiceImpl"></bean> @Service("userinfoService")//此处可以 默认@Service 也可以起别名 public class UserinfoServiceImpl implements UserinfoService { @Autowired private UserinfoMapper userinfoMapper; @Override public List<Userinfo> findAllUsers() { return userinfoMapper.findAllUsers(); } @Override public int addUser(Userinfo user) { return userinfoMapper.addUser(user); } }
UserinfoService.java:
package cn.zzsxt.ssm.service; import java.util.List; import cn.zzsxt.ssm.entity.Userinfo; public interface UserinfoService { /** * 查询用户信息 * @return */ List<Userinfo> findAllUsers(); /** * 新增用户 * @param user * @return */ int addUser(Userinfo user); }
Test.java:
package cn.zzsxt.ssm.test; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.zzsxt.ssm.entity.Userinfo; import cn.zzsxt.ssm.service.UserinfoService; public class Test { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); UserinfoService userinfoService = (UserinfoService)ac.getBean("userinfoService"); List<Userinfo> list = userinfoService.findAllUsers(); for (Userinfo userinfo : list) { System.out.println(userinfo); } } }