spring和mybatis整合
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 加载配置文件 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 数据源,使用dbcp --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxActive" value="10" /> <property name="maxIdle" value="5" /> </bean> <!-- sqlSessinFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 加载mybatis的配置文件 --> <property name="configLocation" value="mybatis/SqlMapConfig.xml" /> <!-- 数据源 --> <property name="dataSource" ref="dataSource" /> </bean> <!-- 原始dao接口 --> <bean id="userDao" class="cn.itcast.ssm.dao.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> <!-- mapper配置 MapperFactoryBean:根据mapper接口生成代理对象 --> <!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> mapperInterface指定mapper接口 <property name="mapperInterface" value="cn.itcast.ssm.mapper.UserMapper"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> --> <!-- mapper批量扫描,从mapper包中扫描出mapper接口,自动创建代理对象并且在spring容器中注册 遵循规范:将mapper.java和mapper.xml映射文件名称保持一致,且在一个目录 中 自动扫描出来的mapper的bean的id为mapper类名(首字母小写) --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 指定扫描的包名 如果扫描多个包,每个包中间使用半角逗号分隔 --> <property name="basePackage" value="cn.itcast.ssm.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> </beans>
sqlMapConfig.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> <!-- 别名定义 --> <typeAliases> <!-- 批量别名定义 指定包名,mybatis自动扫描包中的po类,自动定义别名,别名就是类名(首字母大写或小写都可以) --> <package name="cn.itcast.ssm.po"/> </typeAliases> <!-- 加载 映射文件 --> <mappers> <mapper resource="sqlmap/User.xml"/> <!-- 批量加载mapper 指定mapper接口的包名,mybatis自动扫描包下边所有mapper接口进行加载 遵循一些规范:需要将mapper接口类名和mapper.xml映射文件名称保持一致,且在一个目录 中 上边规范的前提是:使用的是mapper代理方法 和spring整合后,使用mapper扫描器,这里不需要配置了 --> <!-- <package name="cn.itcast.ssm.mapper"/> --> </mappers> </configuration>
package cn.itcast.ssm.dao; import java.util.List; import cn.itcast.ssm.po.User; /** * * <p>Title: UserDao</p> * <p>Description: dao接口,用户管理</p> * <p>Company: www.itcast.com</p> * @author 传智.燕青 * @date 2015-4-22下午2:45:12 * @version 1.0 */ public interface UserDao { //根据id查询用户信息 public User findUserById(int id) throws Exception; } package cn.itcast.ssm.dao; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.support.SqlSessionDaoSupport; import cn.itcast.ssm.po.User; /** * * <p> * Title: UserDaoImpl * </p> * <p> * Description:dao接口实现类 * </p> * <p> * Company: www.itcast.com * </p> * * @author 传智.燕青 * @date 2015-4-22下午2:47:17 * @version 1.0 */ public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao { @Override public User findUserById(int id) throws Exception { //继承SqlSessionDaoSupport,通过this.getSqlSession()得到sqlSessoin SqlSession sqlSession = this.getSqlSession(); User user = sqlSession.selectOne("test.findUserById", id); return user; } }
Mapper
package cn.itcast.ssm.mapper; import cn.itcast.ssm.po.Items; import cn.itcast.ssm.po.ItemsExample; import java.util.List; import org.apache.ibatis.annotations.Param; public interface ItemsMapper { int countByExample(ItemsExample example); int deleteByExample(ItemsExample example); int deleteByPrimaryKey(Integer id); int insert(Items record); int insertSelective(Items record); List<Items> selectByExampleWithBLOBs(ItemsExample example); List<Items> selectByExample(ItemsExample example); Items selectByPrimaryKey(Integer id); int updateByExampleSelective(@Param("record") Items record, @Param("example") ItemsExample example); int updateByExampleWithBLOBs(@Param("record") Items recor
<?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.itcast.ssm.mapper.ItemsMapper" > <resultMap id="BaseResultMap" type="cn.itcast.ssm.po.Items" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="name" property="name" jdbcType="VARCHAR" /> <result column="price" property="price" jdbcType="REAL" /> <result column="pic" property="pic" jdbcType="VARCHAR" /> <result column="createtime" property="createtime" jdbcType="TIMESTAMP" /> </resultMap> <resultMap id="ResultMapWithBLOBs" type="cn.itcast.ssm.po.Items" extends="BaseResultMap" > <result column="detail" property="detail" jdbcType="LONGVARCHAR" /> </resultMap> <sql id="Example_Where_Clause" > <where > <foreach collection="oredCriteria" item="criteria" separator="or" > <if test="criteria.valid" > <trim prefix="(" suffix=")" prefixOverrides="and" > <foreach collection="criteria.criteria" item="criterion" > <choose > <when test="criterion.noValue" > and ${criterion.condition} </when> <when test="criterion.singleValue" > and ${criterion.condition} #{criterion.value} </when> <when test="criterion.betweenValue" > and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} </when> <when test="criterion.listValue" > and ${criterion.condition} <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," > #{listItem} </foreach> </when> </choose> </foreach> </trim> </if> </foreach> </where> </sql> <sql id="Update_By_Example_Where_Clause" > <where > <foreach collection="example.oredCriteria" item="criteria" separator="or" > <if test="criteria.valid" > <trim prefix="(" suffix=")" prefixOverrides="and" > <foreach collection="criteria.criteria" item="criterion" > <choose > <when test="criterion.noValue" > and ${criterion.condition} </when> <when test="criterion.singleValue" > and ${criterion.condition} #{criterion.value} </when> <when test="criterion.betweenValue" > and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} </when> <when test="criterion.listValue" > and ${criterion.condition} <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," > #{listItem} </foreach> </when> </choose> </foreach> </trim> </if> </foreach> </where> </sql> <sql id="Base_Column_List" > id, name, price, pic, createtime </sql> <sql id="Blob_Column_List" > detail </sql> <select id="selectByExampleWithBLOBs" resultMap="ResultMapWithBLOBs" parameterType="cn.itcast.ssm.po.ItemsExample" > select <if test="distinct" > distinct </if> <include refid="Base_Column_List" /> , <include refid="Blob_Column_List" /> from items <if test="_parameter != null" > <include refid="Example_Where_Clause" /> </if> <if test="orderByClause != null" > order by ${orderByClause} </if> </select> <select id="selectByExample" resultMap="BaseResultMap" parameterType="cn.itcast.ssm.po.ItemsExample" > select <if test="distinct" > distinct </if> <include refid="Base_Column_List" /> from items <if test="_parameter != null" > <include refid="Example_Where_Clause" /> </if> <if test="orderByClause != null" > order by ${orderByClause} </if> </select> <select id="selectByPrimaryKey" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> , <include refid="Blob_Column_List" /> from items where id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from items where id = #{id,jdbcType=INTEGER} </delete> <delete id="deleteByExample" parameterType="cn.itcast.ssm.po.ItemsExample" > delete from items <if test="_parameter != null" > <include refid="Example_Where_Clause" /> </if> </delete> <insert id="insert" parameterType="cn.itcast.ssm.po.Items" > insert into items (id, name, price, pic, createtime, detail ) values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{price,jdbcType=REAL}, #{pic,jdbcType=VARCHAR}, #{createtime,jdbcType=TIMESTAMP}, #{detail,jdbcType=LONGVARCHAR} ) </insert> <insert id="insertSelective" parameterType="cn.itcast.ssm.po.Items" > insert into items <trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > id, </if> <if test="name != null" > name, </if> <if test="price != null" > price, </if> <if test="pic != null" > pic, </if> <if test="createtime != null" > createtime, </if> <if test="detail != null" > detail, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="id != null" > #{id,jdbcType=INTEGER}, </if> <if test="name != null" > #{name,jdbcType=VARCHAR}, </if> <if test="price != null" > #{price,jdbcType=REAL}, </if> <if test="pic != null" > #{pic,jdbcType=VARCHAR}, </if> <if test="createtime != null" > #{createtime,jdbcType=TIMESTAMP}, </if> <if test="detail != null" > #{detail,jdbcType=LONGVARCHAR}, </if> </trim> </insert> <select id="countByExample" parameterType="cn.itcast.ssm.po.ItemsExample" resultType="java.lang.Integer" > select count(*) from items <if test="_parameter != null" > <include refid="Example_Where_Clause" /> </if> </select> <update id="updateByExampleSelective" parameterType="map" > update items <set > <if test="record.id != null" > id = #{record.id,jdbcType=INTEGER}, </if> <if test="record.name != null" > name = #{record.name,jdbcType=VARCHAR}, </if> <if test="record.price != null" > price = #{record.price,jdbcType=REAL}, </if> <if test="record.pic != null" > pic = #{record.pic,jdbcType=VARCHAR}, </if> <if test="record.createtime != null" > createtime = #{record.createtime,jdbcType=TIMESTAMP}, </if> <if test="record.detail != null" > detail = #{record.detail,jdbcType=LONGVARCHAR}, </if> </set> <if test="_parameter != null" > <include refid="Update_By_Example_Where_Clause" /> </if> </update> <update id="updateByExampleWithBLOBs" parameterType="map" > update items set id = #{record.id,jdbcType=INTEGER}, name = #{record.name,jdbcType=VARCHAR}, price = #{record.price,jdbcType=REAL}, pic = #{record.pic,jdbcType=VARCHAR}, createtime = #{record.createtime,jdbcType=TIMESTAMP}, detail = #{record.detail,jdbcType=LONGVARCHAR} <if test="_parameter != null" > <include refid="Update_By_Example_Where_Clause" /> </if> </update> <update id="updateByExample" parameterType="map" > update items set id = #{record.id,jdbcType=INTEGER}, name = #{record.name,jdbcType=VARCHAR}, price = #{record.price,jdbcType=REAL}, pic = #{record.pic,jdbcType=VARCHAR}, createtime = #{record.createtime,jdbcType=TIMESTAMP} <if test="_parameter != null" > <include refid="Update_By_Example_Where_Clause" /> </if> </update> <update id="updateByPrimaryKeySelective" parameterType="cn.itcast.ssm.po.Items" > update items <set > <if test="name != null" > name = #{name,jdbcType=VARCHAR}, </if> <if test="price != null" > price = #{price,jdbcType=REAL}, </if> <if test="pic != null" > pic = #{pic,jdbcType=VARCHAR}, </if> <if test="createtime != null" > createtime = #{createtime,jdbcType=TIMESTAMP}, </if> <if test="detail != null" > detail = #{detail,jdbcType=LONGVARCHAR}, </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKeyWithBLOBs" parameterType="cn.itcast.ssm.po.Items" > update items set name = #{name,jdbcType=VARCHAR}, price = #{price,jdbcType=REAL}, pic = #{pic,jdbcType=VARCHAR}, createtime = #{createtime,jdbcType=TIMESTAMP}, detail = #{detail,jdbcType=LONGVARCHAR} where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="cn.itcast.ssm.po.Items" > update items set name = #{name,jdbcType=VARCHAR}, price = #{price,jdbcType=REAL}, pic = #{pic,jdbcType=VARCHAR}, createtime = #{createtime,jdbcType=TIMESTAMP} where id = #{id,jdbcType=INTEGER} </update> </mapper>
d, @Param("example") ItemsExample example); int updateByExample(@Param("record") Items record, @Param("example") ItemsExample example); int updateByPrimaryKeySelective(Items record); int updateByPrimaryKeyWithBLOBs(Items record); int updateByPrimaryKey(Items record); }
package cn.itcast.ssm.mapper; import java.util.List; import cn.itcast.ssm.po.User; import cn.itcast.ssm.po.UserCustom; import cn.itcast.ssm.po.UserQueryVo; /** * * <p>Title: UserMapper</p> * <p>Description: mapper接口,相当 于dao接口,用户管理</p> * <p>Company: www.itcast.com</p> * @author 传智.燕青 * @date 2015-4-22下午2:45:12 * @version 1.0 */ public interface UserMapper { //根据id查询用户信息 public User findUserById(int id) throws Exception; }
<?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命名空间,作用就是对sql进行分类化管理,理解sql隔离 注意:使用mapper代理方法开发,namespace有特殊重要的作用,namespace等于mapper接口地址 --> <mapper namespace="cn.itcast.ssm.mapper.UserMapper"> <select id="findUserById" parameterType="int" resultType="user"> SELECT * FROM USER WHERE id=#{value} </select> </mapper>
package cn.itcast.ssm.po; import java.util.Date; public class Items { private Integer id; private String name; private Float price; private String pic; private Date createtime; private String detail; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name == null ? null : name.trim(); } public Float getPrice() { return price; } public void setPrice(Float price) { this.price = price; } public String getPic() { return pic; } public void setPic(String pic) { this.pic = pic == null ? null : pic.trim(); } public Date getCreatetime() { return createtime; } public void setCreatetime(Date createtime) { this.createtime = createtime; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = detail == null ? null : detail.trim(); } }
package cn.itcast.ssm.po; import java.util.ArrayList; import java.util.Date; import java.util.List; public class ItemsExample { protected String orderByClause; protected boolean distinct; protected List<Criteria> oredCriteria; public ItemsExample() { oredCriteria = new ArrayList<Criteria>(); } public void setOrderByClause(String orderByClause) { this.orderByClause = orderByClause; } public String getOrderByClause() { return orderByClause; } public void setDistinct(boolean distinct) { this.distinct = distinct; } public boolean isDistinct() { return distinct; } public List<Criteria> getOredCriteria() { return oredCriteria; } public void or(Criteria criteria) { oredCriteria.add(criteria); } public Criteria or() { Criteria criteria = createCriteriaInternal(); oredCriteria.add(criteria); return criteria; } public Criteria createCriteria() { Criteria criteria = createCriteriaInternal(); if (oredCriteria.size() == 0) { oredCriteria.add(criteria); } return criteria; } protected Criteria createCriteriaInternal() { Criteria criteria = new Criteria(); return criteria; } public void clear() { oredCriteria.clear(); orderByClause = null; distinct = false; } protected abstract static class GeneratedCriteria { protected List<Criterion> criteria; protected GeneratedCriteria() { super(); criteria = new ArrayList<Criterion>(); } public boolean isValid() { return criteria.size() > 0; } public List<Criterion> getAllCriteria() { return criteria; } public List<Criterion> getCriteria() { return criteria; } protected void addCriterion(String condition) { if (condition == null) { throw new RuntimeException("Value for condition cannot be null"); } criteria.add(new Criterion(condition)); } protected void addCriterion(String condition, Object value, String property) { if (value == null) { throw new RuntimeException("Value for " + property + " cannot be null"); } criteria.add(new Criterion(condition, value)); } protected void addCriterion(String condition, Object value1, Object value2, String property) { if (value1 == null || value2 == null) { throw new RuntimeException("Between values for " + property + " cannot be null"); } criteria.add(new Criterion(condition, value1, value2)); } public Criteria andIdIsNull() { addCriterion("id is null"); return (Criteria) this; } public Criteria andIdIsNotNull() { addCriterion("id is not null"); return (Criteria) this; } public Criteria andIdEqualTo(Integer value) { addCriterion("id =", value, "id"); return (Criteria) this; } public Criteria andIdNotEqualTo(Integer value) { addCriterion("id <>", value, "id"); return (Criteria) this; } public Criteria andIdGreaterThan(Integer value) { addCriterion("id >", value, "id"); return (Criteria) this; } public Criteria andIdGreaterThanOrEqualTo(Integer value) { addCriterion("id >=", value, "id"); return (Criteria) this; } public Criteria andIdLessThan(Integer value) { addCriterion("id <", value, "id"); return (Criteria) this; } public Criteria andIdLessThanOrEqualTo(Integer value) { addCriterion("id <=", value, "id"); return (Criteria) this; } public Criteria andIdIn(List<Integer> values) { addCriterion("id in", values, "id"); return (Criteria) this; } public Criteria andIdNotIn(List<Integer> values) { addCriterion("id not in", values, "id"); return (Criteria) this; } public Criteria andIdBetween(Integer value1, Integer value2) { addCriterion("id between", value1, value2, "id"); return (Criteria) this; } public Criteria andIdNotBetween(Integer value1, Integer value2) { addCriterion("id not between", value1, value2, "id"); return (Criteria) this; } public Criteria andNameIsNull() { addCriterion("name is null"); return (Criteria) this; } public Criteria andNameIsNotNull() { addCriterion("name is not null"); return (Criteria) this; } public Criteria andNameEqualTo(String value) { addCriterion("name =", value, "name"); return (Criteria) this; } public Criteria andNameNotEqualTo(String value) { addCriterion("name <>", value, "name"); return (Criteria) this; } public Criteria andNameGreaterThan(String value) { addCriterion("name >", value, "name"); return (Criteria) this; } public Criteria andNameGreaterThanOrEqualTo(String value) { addCriterion("name >=", value, "name"); return (Criteria) this; } public Criteria andNameLessThan(String value) { addCriterion("name <", value, "name"); return (Criteria) this; } public Criteria andNameLessThanOrEqualTo(String value) { addCriterion("name <=", value, "name"); return (Criteria) this; } public Criteria andNameLike(String value) { addCriterion("name like", value, "name"); return (Criteria) this; } public Criteria andNameNotLike(String value) { addCriterion("name not like", value, "name"); return (Criteria) this; } public Criteria andNameIn(List<String> values) { addCriterion("name in", values, "name"); return (Criteria) this; } public Criteria andNameNotIn(List<String> values) { addCriterion("name not in", values, "name"); return (Criteria) this; } public Criteria andNameBetween(String value1, String value2) { addCriterion("name between", value1, value2, "name"); return (Criteria) this; } public Criteria andNameNotBetween(String value1, String value2) { addCriterion("name not between", value1, value2, "name"); return (Criteria) this; } public Criteria andPriceIsNull() { addCriterion("price is null"); return (Criteria) this; } public Criteria andPriceIsNotNull() { addCriterion("price is not null"); return (Criteria) this; } public Criteria andPriceEqualTo(Float value) { addCriterion("price =", value, "price"); return (Criteria) this; } public Criteria andPriceNotEqualTo(Float value) { addCriterion("price <>", value, "price"); return (Criteria) this; } public Criteria andPriceGreaterThan(Float value) { addCriterion("price >", value, "price"); return (Criteria) this; } public Criteria andPriceGreaterThanOrEqualTo(Float value) { addCriterion("price >=", value, "price"); return (Criteria) this; } public Criteria andPriceLessThan(Float value) { addCriterion("price <", value, "price"); return (Criteria) this; } public Criteria andPriceLessThanOrEqualTo(Float value) { addCriterion("price <=", value, "price"); return (Criteria) this; } public Criteria andPriceIn(List<Float> values) { addCriterion("price in", values, "price"); return (Criteria) this; } public Criteria andPriceNotIn(List<Float> values) { addCriterion("price not in", values, "price"); return (Criteria) this; } public Criteria andPriceBetween(Float value1, Float value2) { addCriterion("price between", value1, value2, "price"); return (Criteria) this; } public Criteria andPriceNotBetween(Float value1, Float value2) { addCriterion("price not between", value1, value2, "price"); return (Criteria) this; } public Criteria andPicIsNull() { addCriterion("pic is null"); return (Criteria) this; } public Criteria andPicIsNotNull() { addCriterion("pic is not null"); return (Criteria) this; } public Criteria andPicEqualTo(String value) { addCriterion("pic =", value, "pic"); return (Criteria) this; } public Criteria andPicNotEqualTo(String value) { addCriterion("pic <>", value, "pic"); return (Criteria) this; } public Criteria andPicGreaterThan(String value) { addCriterion("pic >", value, "pic"); return (Criteria) this; } public Criteria andPicGreaterThanOrEqualTo(String value) { addCriterion("pic >=", value, "pic"); return (Criteria) this; } public Criteria andPicLessThan(String value) { addCriterion("pic <", value, "pic"); return (Criteria) this; } public Criteria andPicLessThanOrEqualTo(String value) { addCriterion("pic <=", value, "pic"); return (Criteria) this; } public Criteria andPicLike(String value) { addCriterion("pic like", value, "pic"); return (Criteria) this; } public Criteria andPicNotLike(String value) { addCriterion("pic not like", value, "pic"); return (Criteria) this; } public Criteria andPicIn(List<String> values) { addCriterion("pic in", values, "pic"); return (Criteria) this; } public Criteria andPicNotIn(List<String> values) { addCriterion("pic not in", values, "pic"); return (Criteria) this; } public Criteria andPicBetween(String value1, String value2) { addCriterion("pic between", value1, value2, "pic"); return (Criteria) this; } public Criteria andPicNotBetween(String value1, String value2) { addCriterion("pic not between", value1, value2, "pic"); return (Criteria) this; } public Criteria andCreatetimeIsNull() { addCriterion("createtime is null"); return (Criteria) this; } public Criteria andCreatetimeIsNotNull() { addCriterion("createtime is not null"); return (Criteria) this; } public Criteria andCreatetimeEqualTo(Date value) { addCriterion("createtime =", value, "createtime"); return (Criteria) this; } public Criteria andCreatetimeNotEqualTo(Date value) { addCriterion("createtime <>", value, "createtime"); return (Criteria) this; } public Criteria andCreatetimeGreaterThan(Date value) { addCriterion("createtime >", value, "createtime"); return (Criteria) this; } public Criteria andCreatetimeGreaterThanOrEqualTo(Date value) { addCriterion("createtime >=", value, "createtime"); return (Criteria) this; } public Criteria andCreatetimeLessThan(Date value) { addCriterion("createtime <", value, "createtime"); return (Criteria) this; } public Criteria andCreatetimeLessThanOrEqualTo(Date value) { addCriterion("createtime <=", value, "createtime"); return (Criteria) this; } public Criteria andCreatetimeIn(List<Date> values) { addCriterion("createtime in", values, "createtime"); return (Criteria) this; } public Criteria andCreatetimeNotIn(List<Date> values) { addCriterion("createtime not in", values, "createtime"); return (Criteria) this; } public Criteria andCreatetimeBetween(Date value1, Date value2) { addCriterion("createtime between", value1, value2, "createtime"); return (Criteria) this; } public Criteria andCreatetimeNotBetween(Date value1, Date value2) { addCriterion("createtime not between", value1, value2, "createtime"); return (Criteria) this; } } public static class Criteria extends GeneratedCriteria { protected Criteria() { super(); } } public static class Criterion { private String condition; private Object value; private Object secondValue; private boolean noValue; private boolean singleValue; private boolean betweenValue; private boolean listValue; private String typeHandler; public String getCondition() { return condition; } public Object getValue() { return value; } public Object getSecondValue() { return secondValue; } public boolean isNoValue() { return noValue; } public boolean isSingleValue() { return singleValue; } public boolean isBetweenValue() { return betweenValue; } public boolean isListValue() { return listValue; } public String getTypeHandler() { return typeHandler; } protected Criterion(String condition) { super(); this.condition = condition; this.typeHandler = null; this.noValue = true; } protected Criterion(String condition, Object value, String typeHandler) { super(); this.condition = condition; this.value = value; this.typeHandler = typeHandler; if (value instanceof List<?>) { this.listValue = true; } else { this.singleValue = true; } } protected Criterion(String condition, Object value) { this(condition, value, null); } protected Criterion(String condition, Object value, Object secondValue, String typeHandler) { super(); this.condition = condition; this.value = value; this.secondValue = secondValue; this.typeHandler = typeHandler; this.betweenValue = true; } protected Criterion(String condition, Object value, Object secondValue) { this(condition, value, secondValue, null); } } }
package cn.itcast.ssm.po; public class Orderdetail { private Integer id; private Integer ordersId; private Integer itemsId; private Integer itemsNum; //明细对应的商品信息 private Items items; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getOrdersId() { return ordersId; } public void setOrdersId(Integer ordersId) { this.ordersId = ordersId; } public Integer getItemsId() { return itemsId; } public void setItemsId(Integer itemsId) { this.itemsId = itemsId; } public Integer getItemsNum() { return itemsNum; } public void setItemsNum(Integer itemsNum) { this.itemsNum = itemsNum; } public Items getItems() { return items; } public void setItems(Items items) { this.items = items; } @Override public String toString() { return "Orderdetail [id=" + id + ", ordersId=" + ordersId + ", itemsId=" + itemsId + ", itemsNum=" + itemsNum + "]"; } }
package cn.itcast.ssm.po; import java.util.Date; import java.util.List; public class Orders { private Integer id; private Integer userId; private String number; private Date createtime; private String note; //用户信息 private User user; //订单明细 private List<Orderdetail> orderdetails; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number == null ? null : number.trim(); } public Date getCreatetime() { return createtime; } public void setCreatetime(Date createtime) { this.createtime = createtime; } public String getNote() { return note; } public void setNote(String note) { this.note = note == null ? null : note.trim(); } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public List<Orderdetail> getOrderdetails() { return orderdetails; } public void setOrderdetails(List<Orderdetail> orderdetails) { this.orderdetails = orderdetails; } }
package cn.itcast.ssm.po; /** * * <p>Title: OrdersCustom</p> * <p>Description: 订单的扩展类</p> * <p>Company: www.itcast.com</p> * @author 传智.燕青 * @date 2015-4-23上午10:25:47 * @version 1.0 */ //通过此类映射订单和用户查询的结果,让此类继承包括 字段较多的pojo类 public class OrdersCustom extends Orders{ //添加用户属性 /*USER.username, USER.sex, USER.address */ private String username; private String sex; private String address; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
package cn.itcast.ssm.po; import java.io.Serializable; import java.util.Date; import java.util.List; /** * * <p>Title: User</p> * <p>Description:用户po </p> * <p>Company: www.itcast.com</p> * @author 传智.燕青 * @date 2015-4-22上午10:24:16 * @version 1.0 */ public class User implements Serializable { //属性名和数据库表的字段对应 private int id; private String username;// 用户姓名 private String sex;// 性别 private Date birthday;// 生日 private String address;// 地址 //用户创建的订单列表 private List<Orders> ordersList; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", sex=" + sex + ", birthday=" + birthday + ", address=" + address + "]"; } public List<Orders> getOrdersList() { return ordersList; } public void setOrdersList(List<Orders> ordersList) { this.ordersList = ordersList; } }
package cn.itcast.ssm.po; /** * * <p>Title: UserCustom</p> * <p>Description: 用户的扩展类</p> * <p>Company: www.itcast.com</p> * @author 传智.燕青 * @date 2015-4-22下午4:26:31 * @version 1.0 */ public class UserCustom extends User{ //可以扩展用户的信息 }
package cn.itcast.ssm.po; import java.util.List; /** * * <p>Title: UserQueryVo</p> * <p>Description:包装类型 </p> * <p>Company: www.itcast.com</p> * @author 传智.燕青 * @date 2015-4-22下午4:24:44 * @version 1.0 */ public class UserQueryVo { //传入多个id private List<Integer> ids; //在这里包装所需要的查询条件 //用户查询条件 private UserCustom userCustom; public UserCustom getUserCustom() { return userCustom; } public void setUserCustom(UserCustom userCustom) { this.userCustom = userCustom; } public List<Integer> getIds() { return ids; } public void setIds(List<Integer> ids) { this.ids = ids; } //可以包装其它的查询条件,订单、商品 //.... }
package cn.itcast.ssm.dao; import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.itcast.ssm.po.User; public class UserDaoImplTest { private ApplicationContext applicationContext; //在setUp这个方法得到spring容器 @Before public void setUp() throws Exception { applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml"); } @Test public void testFindUserById() throws Exception { UserDao userDao = (UserDao) applicationContext.getBean("userDao"); //调用userDao的方法 User user = userDao.findUserById(1); System.out.println(user); } }
package cn.itcast.ssm.mapper; import static org.junit.Assert.*; import java.util.List; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.itcast.ssm.po.Items; import cn.itcast.ssm.po.ItemsExample; public class ItemsMapperTest { private ApplicationContext applicationContext; private ItemsMapper itemsMapper; //在setUp这个方法得到spring容器 @Before public void setUp() throws Exception { applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml"); itemsMapper = (ItemsMapper) applicationContext.getBean("itemsMapper"); } //根据主键删除 @Test public void testDeleteByPrimaryKey() { } //插入 @Test public void testInsert() { //构造 items对象 Items items = new Items(); items.setName("手机"); items.setPrice(999f); itemsMapper.insert(items); } //自定义条件查询 @Test public void testSelectByExample() { ItemsExample itemsExample = new ItemsExample(); //通过criteria构造查询条件 ItemsExample.Criteria criteria = itemsExample.createCriteria(); criteria.andNameEqualTo("笔记本3"); //可能返回多条记录 List<Items> list = itemsMapper.selectByExample(itemsExample); System.out.println(list); } //根据主键查询 @Test public void testSelectByPrimaryKey() { Items items = itemsMapper.selectByPrimaryKey(1); System.out.println(items); } //更新数据 @Test public void testUpdateByPrimaryKey() { //对所有字段进行更新,需要先查询出来再更新 Items items = itemsMapper.selectByPrimaryKey(1); items.setName("水杯"); itemsMapper.updateByPrimaryKey(items); //如果传入字段不空为才更新,在批量更新中使用此方法,不需要先查询再更新 //itemsMapper.updateByPrimaryKeySelective(record); } }
package cn.itcast.ssm.mapper; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.itcast.ssm.po.User; public class UserMapperTest { private ApplicationContext applicationContext; //在setUp这个方法得到spring容器 @Before public void setUp() throws Exception { applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml"); } @Test public void testFindUserById() throws Exception { UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper"); User user = userMapper.findUserById(1); System.out.println(user); } }