this.item = item;
}
}
[](()3、创建User类
package com.itzheng.springmvc.pojo;
import java.util.Date;
public class User {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username == null ? null : username.trim();
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex == null ? null : sex.trim();
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address == null ? null : address.trim();
}
}
[](()4、创建UserExample类
package com.itzheng.springmvc.pojo;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
public class UserExample {
protected String orderByClause;
protected boolean distinct;
protected List oredCriteria;
public UserExample() {
oredCriteria = new ArrayList();
}
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 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 criteria;
protected GeneratedCriteria() {
super();
criteria = new ArrayList();
}
public boolean isValid() {
return criteria.size() > 0;
}
public List getAllCriteria() {
return criteria;
}
public List 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));
}
protected void addCriterionForJDBCDate(String condition, Date value, String property) {
if (value == null) {
throw new RuntimeException(“Value for " + property + " cannot be null”);
}
addCriterion(condition, new java.sql.Date(value.getTime()), property);
}
protected void addCriterionForJDBCDate(String condition, List values, String property) {
if (values == null || values.size() == 0) {
throw new RuntimeException(“Value list for " + property + " cannot be null or empty”);
}
List<java.sql.Date> dateList = new ArrayList<java.sql.Date>();
Iterator iter = values.iterator();
while (iter.hasNext()) {
dateList.add(new java.sql.Date(iter.next().getTime()));
}
addCriterion(condition, dateList, property);
}
protected void addCriterionForJDBCDate(String condition, Date value1, Date value2, String property) {
if (value1 == null || value2 == null) {
throw new RuntimeException(“Between values for " + property + " cannot be null”);
}
addCriterion(condition, new java.sql.Date(value1.getTime()), new java.sql.Date(value2.getTime()), property);
}
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 values) {
addCriterion(“id in”, values, “id”);
return (Criteria) this;
}
public Criteria andIdNotIn(List 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 andUsernameIsNull() {
addCriterion(“username is null”);
return (Criteria) this;
}
public Criteria andUsernameIsNotNull() {
addCriterion(“username is not null”);
return (Criteria) this;
}
public Criteria andUsernameEqualTo(String value) {
addCriterion(“username =”, value, “username”);
return (Criteria) this;
}
public Criteria andUsernameNotEqualTo(String value) {
addCriterion(“username <>”, value, “username”);
return (Criteria) this;
}
public Criteria andUsernameGreaterThan(String value) {
addCriterion(“username >”, value, “username”);
return (Criteria) this;
}
public Criteria andUsernameGreaterThanOrEqualTo(String value) {
addCriterion(“username >=”, value, “username”);
return (Criteria) this;
}
public Criteria andUsernameLessThan(String value) {
addCriterion(“username <”, value, “username”);
return (Criteria) this;
}
public Criteria andUsernameLessThanOrEqualTo(String value) {
addCriterion(“username <=”, value, “username”);
return (Criteria) this;
}
public Criteria andUsernameLike(String value) {
addCriterion(“username like”, value, “username”);
return (Criteria) this;
}
public Criteria andUsernameNotLike(String value) {
addCriterion(“username not like”, value, “username”);
return (Criteria) this;
}
public Criteria andUsernameIn(List values) {
addCriterion(“username in”, values, “username”);
return (Criteria) this;
}
public Criteria andUsernameNotIn(List values) {
addCriterion(“username not in”, values, “username”);
return (Criteria) this;
}
public Criteria andUsernameBetween(String value1, String value2) {
addCriterion(“username between”, value1, value2, “username”);
return (Criteria) this;
}
public Criteria andUsernameNotBetween(String value1, String value2) {
addCriterion(“username not between”, value1, value2, “username”);
return (Criteria) this;
}
public Criteria andBirthdayIsNull() {
addCriterion(“birthday is null”);
return (Criteria) this;
}
public Criteria andBirthdayIsNotNull() {
addCriterion(“birthday is not null”);
return (Criteria) this;
}
public Criteria andBirthdayEqualTo(Date value) {
addCriterionForJDBCDate(“birthday =”, value, “birthday”);
return (Criteria) this;
}
public Criteria andBirthdayNotEqualTo(Date value) {
addCriterionForJDBCDate(“birthday <>”, value, “birthday”);
return (Criteria) this;
}
public Criteria andBirthdayGreaterThan(Date value) {
addCriterionForJDBCDate(“birthday >”, value, “birthday”);
return (Criteria) this;
}
public Criteria andBirthdayGreaterThanOrEqualTo(Date value) {
addCriterionForJDBCDate(“birthday >=”, value, “birthday”);
return (Criteria) this;
}
public Criteria andBirthdayLessThan(Date value) {
addCriterionForJDBCDate(“birthday <”, value, “birthday”);
return (Criteria) this;
}
public Criteria andBirthdayLessThanOrEqualTo(Date value) {
addCriterionForJDBCDate(“birthday <=”, value, “birthday”);
return (Criteria) this;
}
public Criteria andBirthdayIn(List values) {
addCriterionForJDBCDate(“birthday in”, values, “birthday”);
return (Criteria) this;
}
public Criteria andBirthdayNotIn(List values) {
addCriterionForJDBCDate(“birthday not in”, values, “birthday”);
return (Criteria) this;
}
public Criteria andBirthdayBetween(Date value1, Date value2) {
addCriterionForJDBCDate(“birthday between”, value1, value2, “birthday”);
return (Criteria) this;
}
public Criteria andBirthdayNotBetween(Date value1, Date value2) {
addCriterionForJDBCDate(“birthday not between”, value1, value2, “birthday”);
return (Criteria) this;
}
public Criteria andSexIsNull() {
addCriterion(“sex is null”);
return (Criteria) this;
}
public Criteria andSexIsNotNull() {
addCriterion(“sex is not null”);
return (Criteria) this;
}
public Criteria andSexEqualTo(String value) {
addCriterion(“sex =”, value, “sex”);
return (Criteria) this;
}
public Criteria andSexNotEqualTo(String value) {
addCriterion(“sex <>”, value, “sex”);
return (Criteria) this;
}
public Criteria andSexGreaterThan(String value) {
addCriterion(“sex >”, value, “sex”);
return (Criteria) this;
}
public Criteria andSexGreaterThanOrEqualTo(String value) {
addCriterion(“sex >=”, value, “sex”);
return (Criteria) this;
}
public Criteria andSexLessThan(String value) {
addCriterion(“sex <”, value, “sex”);
return (Criteria) this;
}
public Criteria andSexLessThanOrEqualTo(String value) {
addCriterion(“sex <=”, value, “sex”);
return (Criteria) this;
}
public Criteria andSexLike(String value) {
addCriterion(“sex like”, value, “sex”);
return (Criteria) this;
}
public Criteria andSexNotLike(String value) {
addCriterion(“sex not like”, value, “sex”);
return (Criteria) this;
}
public Criteria andSexIn(List values) {
addCriterion(“sex in”, values, “sex”);
return (Criteria) this;
}
public Criteria andSexNotIn(List values) {
addCriterion(“sex not in”, values, “sex”);
return (Criteria) this;
}
public Criteria andSexBetween(String value1, String value2) {
addCriterion(“sex between”, value1, value2, “sex”);
return (Criteria) this;
}
public Criteria andSexNotBetween(String value1, String value2) {
addCriterion(“sex not between”, value1, value2, “sex”);
return (Criteria) this;
}
public Criteria andAddressIsNull() {
addCriterion(“address is null”);
return (Criteria) this;
}
public Criteria andAddressIsNotNull() {
addCriterion(“address is not null”);
return (Criteria) this;
}
public Criteria andAddressEqualTo(String value) {
addCriterion(“address =”, value, “address”);
return (Criteria) this;
}
public Criteria andAddressNotEqualTo(String value) {
addCriterion(“address <>”, value, “address”);
return (Criteria) this;
}
public Criteria andAddressGreaterThan(String value) {
addCriterion(“address >”, value, “address”);
return (Criteria) this;
}
public Criteria andAddressGreaterThanOrEqualTo(String value) {
addCriterion(“address >=”, value, “address”);
return (Criteria) this;
}
public Criteria andAddressLessThan(String value) {
addCriterion(“address <”, value, “address”);
return (Criteria) this;
}
public Criteria andAddressLessThanOrEqualTo(String value) {
addCriterion(“address <=”, value, “address”);
return (Criteria) this;
}
public Criteria andAddressLike(String value) {
addCriterion(“address like”, value, “address”);
return (Criteria) this;
}
public Criteria andAddressNotLike(String value) {
addCriterion(“address not like”, value, “address”);
return (Criteria) this;
}
public Criteria andAddressIn(List values) {
addCriterion(“address in”, values, “address”);
return (Criteria) this;
}
public Criteria andAddressNotIn(List values) {
addCriterion(“address not in”, values, “address”);
return (Criteria) this;
}
public Criteria andAddressBetween(String value1, String value2) {
addCriterion(“address between”, value1, value2, “address”);
return (Criteria) this;
}
public Criteria andAddressNotBetween(String value1, String value2) {
addCriterion(“address not between”, value1, value2, “address”);
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);
}
}
}
[](()5、创建SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>[](()6、创建spring文件夹并创建applicationContext-dao.xml
a)数据库连接池
b)SqlSessionFactory对象,需要spring和mybatis整合包下的。
c)配置mapper文件扫描器
<?xml version="1.0" encoding="UTF-8"?><beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:context=“http://www.springframework.org/schema/context” xmlns:p=“http://www.springframework.org/schema/p”
xmlns:aop=“http://www.springframework.org/schema/aop” xmlns:tx=“http://www.springframework.org/schema/tx”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<context:property-placeholder location=“classpath:jdbc.properties” />
<bean id=“dataSource” class=“org.apache.commons.dbcp.BasicDataSource”
destroy-method=“close”>
[](()7、jdbc.properties和log4j.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
Global logging configuration
log4j.rootLogger=DEBUG, stdout
Console output…
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
[](()8、创建mapper文件夹以及对应的类和映射
[](()(1)创建ItemMapper接口和对应的方法
package com.itzheng.springmvc.mapper;
import com.itzheng.springmvc.pojo.Item;
import com.itzheng.springmvc.pojo.ItemExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface ItemMapper {
int countByExample(ItemExample example);
int deleteByExample(ItemExample example);
int deleteByPrimaryKey(Integer id);
int insert(Item record);
int insertSelective(Item record);
List selectByExample(ItemExample example);
Item selectByPrimaryKey(Integer id);
int updateByExampleSelective(@Param(“record”) Item record, @Param(“example”) ItemExample example);
int updateByExample(@Param(“record”) Item record, @Param(“example”) ItemExample example);
int updateByPrimaryKeySelective(Item record);
int updateByPrimaryKey(Item record);
}
[](()(2)创建ItemMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>and ${criterion.condition}
and ${criterion.condition} #{criterion.value}
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
and ${criterion.condition}
#{listItem}
and ${criterion.condition}
and ${criterion.condition} #{criterion.value}
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
and ${criterion.condition}
#{listItem}
id, name, price, detail, pic, createtime
select
distinct
from item
order by ${orderByClause}
select
from item
where id = #{id,jdbcType=INTEGER}
delete from item
where id = #{id,jdbcType=INTEGER}
delete from item
insert into item (id, name, price,
detail, pic, createtime
)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{price,jdbcType=REAL},
#{detail,jdbcType=VARCHAR}, #{pic,jdbcType=VARCHAR}, #{createtime,jdbcType=TIMESTAMP}
)
insert into item
id,
name,
price,
detail,
pic,
createtime,
#{id,jdbcType=INTEGER},
#{name,jdbcType=VARCHAR},
#{price,jdbcType=REAL},
#{detail,jdbcType=VARCHAR},
#{pic,jdbcType=VARCHAR},
#{createtime,jdbcType=TIMESTAMP},
select count(*) from item
update item
id = #{record.id,jdbcType=INTEGER},
name = #{record.name,jdbcType=VARCHAR},
price = #{record.price,jdbcType=REAL},
detail = #{record.detail,jdbcType=VARCHAR},
pic = #{record.pic,jdbcType=VARCHAR},
createtime = #{record.createtime,jdbcType=TIMESTAMP},
update item
set id = #{record.id,jdbcType=INTEGER},
name = #{record.name,jdbcType=VARCHAR},
price = #{record.price,jdbcType=REAL},
detail = #{record.detail,jdbcType=VARCHAR},
pic = #{record.pic,jdbcType=VARCHAR},
createtime = #{record.createtime,jdbcType=TIMESTAMP}
update item
name = #{name,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL},
detail = #{detail,jdbcType=VARCHAR},
pic = #{pic,jdbcType=VARCHAR},
createtime = #{createtime,jdbcType=TIMESTAMP},
where id = #{id,jdbcType=INTEGER}
update item
set name = #{name,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL},
detail = #{detail,jdbcType=VARCHAR},
pic = #{pic,jdbcType=VARCHAR},
createtime = #{createtime,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=INTEGER}
[](()(3)创建UserMapper.java
package com.itzheng.springmvc.mapper;
import com.itzheng.springmvc.pojo.User;
import com.itzheng.springmvc.pojo.UserExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface UserMapper {
int countByExample(UserExample example);
int deleteByExample(UserExample example);
int deleteByPrimaryKey(Integer id);
int insert(User record);
int insertSelective(User record);
List selectByExample(UserExample example);
User selectByPrimaryKey(Integer id);
int updateByExampleSelective(@Param(“record”) User record, @Param(“example”) UserExample example);
int updateByExample(@Param(“record”) User record, @Param(“example”) UserExample example);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
}
[](()(4)创建UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>and ${criterion.condition}
and ${criterion.condition} #{criterion.value}
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
and ${criterion.condition}
#{listItem}
and ${criterion.condition}
and ${criterion.condition} #{criterion.value}
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
and ${criterion.condition}
#{listItem}
id, username, birthday, sex, address
select
distinct
from user
order by ${orderByClause}
select
from user
where id = #{id,jdbcType=INTEGER}
delete from user
where id = #{id,jdbcType=INTEGER}
delete from user
insert into user (id, username, birthday,
sex, address)
values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{birthday,jdbcType=DATE},
#{sex,jdbcType=CHAR}, #{address,jdbcType=VARCHAR})
insert into user
id,
username,
birthday,
sex,
address,
#{id,jdbcType=INTEGER},
#{username,jdbcType=VARCHAR},
#{birthday,jdbcType=DATE},
#{sex,jdbcType=CHAR},
#{address,jdbcType=VARCHAR},
select count(*) from user
update user
id = #{record.id,jdbcType=INTEGER},
username = #{record.username,jdbcType=VARCHAR},
birthday = #{record.birthday,jdbcType=DATE},
sex = #{record.sex,jdbcType=CHAR},
address = #{record.address,jdbcType=VARCHAR},
update user
set id = #{record.id,jdbcType=INTEGER},
username = #{record.username,jdbcType=VARCHAR},
birthday = #{record.birthday,jdbcType=DATE},
sex = #{record.sex,jdbcType=CHAR},
address = #{record.address,jdbcType=VARCHAR}
update user
username = #{username,jdbcType=VARCHAR},
birthday = #{birthday,jdbcType=DATE},
sex = #{sex,jdbcType=CHAR},
address = #{address,jdbcType=VARCHAR},
where id = #{id,jdbcType=INTEGER}
update user
set username = #{username,jdbcType=VARCHAR},
birthday = #{birthday,jdbcType=DATE},
sex = #{sex,jdbcType=CHAR},
address = #{address,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
[](()二、创建Service层
[](()(一)创建Service的接口和对应的实现类
[](()1、创建对应的接口的方法
package com.itzheng.springmvc.service;
import java.util.List;
import com.itzheng.springmvc.pojo.Item;
/**
-
@author Steven
*/
public interface ItemService {
/**
- @return
*/
List getItemList();
/**
-
@param id
-
@return
*/
Item getItemById(Integer id);
/**
- @param item
*/
void updateItem(Item item);
}
[](()2、创建对应的实现类和对象与的方法
package com.itzheng.springmvc.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.itzheng.springmvc.mapper.ItemMapper;
import com.itzheng.springmvc.pojo.Item;
import com.itzheng.springmvc.service.ItemService;
@Service
public class ItemServiceImpl implements ItemService {
@Autowired
private ItemMapper itemMapper;
@Override
public List getItemList() {
return itemMapper.selectByExample(null);
}
@Override
public Item getItemById(Integer id) {
return itemMapper.selectByPrimaryKey(id);
}
@Override
public void updateItem(Item item) {
itemMapper.updateByPrimaryKeySelective(item);
}
}
[](()(二)Service对应的配置文件
[](()1、applicationContext-service.xml包扫描器,扫描@service注解的类。
<?xml version="1.0" encoding="UTF-8"?><beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:context=“http://www.springframework.org/schema/context” xmlns:p=“http://www.springframework.org/schema/p”
xmlns:aop=“http://www.springframework.org/schema/aop” xmlns:tx=“http://www.springframework.org/schema/tx”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<context:component-scan base-package=“com.itzheng.springmvc.service”/>
[](()2、applicationContext-trans.xml配置事务。
<?xml version="1.0" encoding="UTF-8"?><beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:context=“http://www.springframework.org/schema/context” xmlns:p=“http://www.springframework.org/schema/p”
xmlns:aop=“http://www.springframework.org/schema/aop” xmlns:tx=“http://www.springframework.org/schema/tx”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<bean id=“transactionManager”
class=“org.springframework.jdbc.datasource.DataSourceTransactionManager”>
<tx:advice id=“txAdvice” transaction-manager=“transactionManager”>
tx:attributes
<tx:method name=“save*” propagation=“REQUIRED” />
<tx:method name=“insert*” propagation=“REQUIRED” />
<tx:method name=“delete*” propagation=“REQUIRED” />
<tx:method name=“update*” propagation=“REQUIRED” />
<tx:method name=“find*” propagation=“SUPPORTS” read-only=“true” />
<tx:method name=“get*” propagation=“SUPPORTS” read-only=“true” />
<tx:method name=“query*” propagation=“SUPPORTS” read-only=“true” />
</tx:attributes>
</tx:advice>
aop:config
<aop:advisor advice-ref=“txAdvice”
pointcut=“execution(* com.itzheng.springmvc.service..(…))” />
</aop:config>
[](()三、Controller层:
Springmvc.xml
[](()1、包扫描器,扫描@Controller注解的类。
[](()2、配置注解驱动。
[](()3、视图解析器
package com.itzheng.springmvc.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.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.itzheng.springmvc.pojo.Item;
import com.itzheng.springmvc.pojo.QueryVo;
import com.itzheng.springmvc.service.ItemService;
@Controller
public class ItemController {
@Autowired
private ItemService itemService;
@RequestMapping(“itemList”)
public ModelAndView itemList() {
ModelAndView mav = new ModelAndView();
List itemList = itemService.getItemList();
mav.addObject(“itemList”, itemList);
// mav.setViewName(“/WEB-INF/jsp/itemList.jsp”);
mav.setViewName(“itemList”);
return mav;
}
/**
-
跟据ID查询商品信息,跳转修改商品页面
-
演示默认支持的参数传递
-
Model/ModelMap返回数据模型
-
@param request
-
@param response
-
@param session
-
@return
*/
/*@RequestMapping(“itemEdit”)
public String itemEdit(Model model,ModelMap modelMap,HttpServletRequest request, HttpServletResponse response, HttpSession session) {
String idStr = request.getParameter(“id”);
System.out.println(“response:” + response);
System.out.println(“session:” + session);
// 查询商品信息
Item item = itemService.getItemById(new Integer(idStr));
//model返回数据模型
model.addAttribute(“item”, item);
//mav.addObject(“item”, item);
return “itemEdit”;
}*/
@RequestMapping(“itemEdit”)
public String itemEdit(Model model,@RequestParam(value=“id”,required=true,defaultValue=“1”) Integer ids) {
// 查询商品信息
Item item = itemService.getItemById(ids);
//model返回数据模型
model.addAttribute(“item”, item);
//mav.addObject(“item”, item);
return “itemEdit”;
}
/**
-
修改商品
-
演示pojo参数绑定
-
@param item
-
@return
*/
@RequestMapping(“updateItem”)
public String updateItem(Item item,Model model){
itemService.updateItem(item);
model.addAttribute(“item”, item);
model.addAttribute(“msg”, “修改商品信息成功”);
return “itemEdit”;
}
@RequestMapping(“queryItem”)
public String queryItem(QueryVo vo,Model model){
if(vo.getItem() != null){
System.out.println(vo.getItem());
}
//模拟搜索商品
List itemList = itemService.getItemList();
model.addAttribute(“itemList”, itemList);
return “itemList”;
}
}
Springmvc.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:p=“http://www.springframework.org/schema/p”
xmlns:context=“http://www.springframework.org/schema/context”
xmlns:mvc=“http://www.springframework.org/schema/mvc”
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package=“com.itzheng.springmvc.controller” />
<mvc:annotation-driven />
[](()四、Web.xml
[](()1.配置spring容量监听器
[](()2.配置前端控制器
<?xml version="1.0" encoding="UTF-8"?>03-springmvc-mybatis
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
contextConfigLocation
classpath:spring/applicationContext*.xml
org.springframework.web.context.ContextLoaderListener
encoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encoding
/*