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); } }
相关推荐
【传智燕青 mybatis 课堂笔记】是传智播客燕青老师关于MyBatis这一流行Java持久层框架的精彩教学内容。MyBatis是一个轻量级的框架,它解决了传统的基于JDBC的繁琐代码问题,使得SQL与Java代码能够更加紧密地结合,...
#### 三、MyBatis解决方案 针对上述问题,MyBatis提供了一系列解决方案: 1. **数据库连接管理**:MyBatis可以通过配置文件或者代码来设置数据库连接池,有效地解决了连接创建和释放的问题,提高了数据库访问效率...
在本套"传智播客 燕青 spring_2016 视频教程 第3天"中,燕青老师深入浅出地讲解了Spring框架的核心知识与实践技巧,这是针对2016年版的Spring技术进行的一次详细教学。教程内容丰富,旨在帮助学员快速掌握Spring框架...
《传智播客 燕青 spring_2016 视频教程 第2天》是一份由知名教育机构传智播客推出的Spring框架教学资源,由经验丰富的讲师燕青主讲。本教程针对2016年的技术环境,旨在帮助学员深入理解和掌握Spring框架的核心概念和...
spring mybatis shiro 共6天 8部分 第6部分 传智 燕青主讲 非常好
在本教程中,我们主要关注 Spring MVC 的核心概念、工作原理以及如何与 MyBatis 集成进行数据持久化。 **Spring MVC 架构** 1. **DispatcherServlet**:是 Spring MVC 的核心,作为前端控制器,它负责接收请求,...
spring mybatis shiro 共6天 8部分 第5部分 传智 燕青主讲 非常好
spring mybatis shiro 共6天 8部分 第3部分 传智 燕青主讲 非常好
spring mybatis shiro 共6天 8部分 第4部分 传智 燕青主讲 非常好
spring mybatis shiro 共6天 8部分 第8部分 传智 燕青主讲 非常好
spring mybatis shiro 共6天 8部分 第5部分 传智 燕青主讲 非常好
【传智mybatis MVC教案及笔记 燕青】涵盖了Java Web开发中的核心框架——MyBatis和MVC模式的应用,这些内容对于深入理解和实践Java后端开发至关重要。MyBatis是一个优秀的持久层框架,它支持定制化SQL、存储过程以及...
传智播客的燕青老师提供的"SpringMVC+Mybatis由浅入深"教程涵盖了这两个框架的基本概念、核心组件、配置、实战应用等多个方面。课堂笔记详细记录了每天的学习内容,帮助学习者巩固理论知识,理解每个技术点的应用...
9. **MyBatis集成**:作为此次资料的主体,MyBatis是另一款流行的持久层框架,会介绍如何与SpringMVC整合,实现DAO层操作,包括SqlSession管理、Mapper接口的使用等。 源代码部分可能包含上述概念的实际示例,帮助...
这个“传智播客 Springmvc+Mybatis由浅入深 教案和笔记”涵盖了SpringMVC与MyBatis的基础知识和进阶内容,适合初学者和有一定经验的开发者。配合B站上的配套视频,能够更直观地理解这两个框架的工作原理和实际应用。...
4. **SpringMVC与MyBatis整合** - SpringMVC可以与MyBatis无缝集成,实现数据库操作,通过Spring的DataSource和SqlSessionFactoryBean配置数据源和会话工厂。 - Controller中注入Service,Service中通过MyBatis的...
7. **传智播客燕青**:标签中的"传智播客燕青"可能指的是某位教师或教育机构,他们在Java Web教学中提供了关于SSM框架的教程或课程,这个学习工程可能是课程的一部分,旨在帮助学生实践所学知识。 通过深入研究和...
SSM整合是Java Web开发中常见的一种架构组合,由Spring MVC、MyBatis和Spring三大框架构成。...同时,对传智燕青的代码进行小修改,意味着你还可以了解到前辈的经验和改进之处,这对于个人技能的提升非常有帮助。
SSM框架,全称为Spring、SpringMVC和MyBatis的组合,是Java Web开发中常用的三大组件。这个"ssm框架学习sql数据"的资料很可能是为了帮助开发者理解和掌握如何在SSM框架中进行数据库操作。下面我们将深入探讨SSM框架...