java:Mybatis框架3(二级缓存,延时和积极加载,SSI(Ibatis)集成,SSM集成)
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);
}
}
}
java:Mybatis框架3(二级缓存,延时和积极加载,SSI(Ibatis)集成,SSM集成)的更多相关文章
- mybatis整合redis二级缓存
mybatis默认开启了二级缓存功能,在mybatis主配置文件中,将cacheEnabled设置成false,则会关闭二级缓存功能 <settings> <!--二级缓存默认开启, ...
- Spring + MySQL + Mybatis + Redis【二级缓存】
一.Redis环境 Redis 官网 :http://redis.io/ windows下载:https://github.com/dmajkic/redis/downloads 1.文件解压缩 2. ...
- 【MyBatis学习13】MyBatis中的二级缓存
1. 二级缓存的原理 前面介绍了,mybatis中的二级缓存是mapper级别的缓存,值得注意的是,不同的mapper都有一个二级缓存,也就是说,不同的mapper之间的二级缓存是互不影响的.为了更加 ...
- Mybatis一级、二级缓存
Mybatis一级.二级缓存 一级缓存 首先做一个测试,创建一个mapper配置文件和mapper接口,我这里用了最简单的查询来演示. <mapper namespace="c ...
- Mybatis使用Redis二级缓存
在Mybatis中允许开发者自定义自己的缓存,本文将使用Redis作为Mybatis的二级缓存.在Mybatis中定义二级缓存,需要如下配置: 1. MyBatis支持二级缓存的总开关:全局配置变量参 ...
- Google官方网络框架-Volley的使用解析Json以及加载网络图片方法
Google官方网络框架-Volley的使用解析Json以及加载网络图片方法 Volley是什么? Google I/O 大会上,Google 推出 Volley的一个网络框架 Volley适合什么场 ...
- 二级ul li元素动态加载click事件
一.代码 html代码: <ul class="id1" id="id1" style="width:84%; height:75%;overf ...
- Java Hello World 错误 找不到或无法加载主类
Java 有几年没用了 生疏了好多 最近又捡起来 结果第一个Hello World 就在黑窗口内报错! 遇到几个小问题. 1. 安装JDK后 在 CMD 中 执行 java -version 正常 因 ...
- 【全网最优方法】JAVA初学:错误: 找不到或无法加载主类HelloWorld
JAVA初学:错误: 找不到或无法加载主类 HelloWorld 我这是看的黑马2019网课(B站)出现的问题. 放一下别人的图,我也是大概的问题:就是javac没问题,java却无论怎么弄都报错. ...
- keep-alive vue组件缓存避免多次加载相应的组件
keep-alive vue组件缓存避免多次加载相应的组件
随机推荐
- maven生成jar包编码问题
要做一个jar文件供外部调用,此jar的源代码中注释为中文,用maven打包后在其它工程中导入后总不能正常显示中文,记录解决方法如下: 在pom.xml中设置默认编码类型为UTF-8: <pro ...
- Codeforces Round #608 (Div. 2) D. Portals
链接: https://codeforces.com/contest/1271/problem/D 题意: You play a strategic video game (yeah, we ran ...
- 在linux 下配置firewalld
查看firewalld 是否开始与运行 以下两种方式都可以 systemctl status firewalld.service firewall-cmd --state 查看所有打开的端口 以下两种 ...
- EasyUI ComboBox ajax
ajax请求数据 $.post("get_select", { "type1" : "mytype" }, function(data) { ...
- jquery input选择器 语法
jquery input选择器 语法 作用::input 选择器选取表单元素.该选择器同样适用于 <button> 元素.大理石平台价格表 语法:$(":input") ...
- react页面跳转
<Button style={{backgroundColor:'#F0F2F5'}} onClick={()=>{window.location.href="https://b ...
- ubuntu编译安装openssl
http://blog.bccn.net/%E9%9D%99%E5%A4%9C%E6%80%9D/66642 su root 不然权限不够 cd /usr/src wget https://www. ...
- 深度学习笔记(十二)车道线检测 LaneNet
论文:Towards End-to-End Lane Detection: an Instance Segmentation Approach 代码:https://github.com/MaybeS ...
- CentOS6.5卸载自带的Mysql软件
现想要在这家的服务器上安装Mysql集群,发现之前安装操作系统的时候顺便把MySql默认安装,所以需要将它先卸载掉. 1,查找已安装的mysql版本 [root@leader ~]# rpm -qa| ...
- [JZOJ5399]:Confess(随机化)
题目描述 小$w$隐藏的心绪已经难以再隐藏下去了. 小$w$有$n+1$(保证$n$为偶数)个心绪,每个都包含了$[1,2n]$的一个大小为$n$的子集. 现在他要找到隐藏的任意两个心绪,使得他们的交 ...