SpringMVC快速入门(4)SpringMVC整合Mybatis

本文详细介绍了如何将SpringMVC与Mybatis进行整合。从创建User类和UserExample类开始,逐步讲解了SqlMapConfig.xml的配置、Spring配置文件的设置,以及Service接口和实现类的创建。此外,还涉及到了事务管理和Controller的配置,是学习SpringMVC与Mybatis整合的实用指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

/*

本项目是用的spring springMVC myBatis框架,前段没用框架,只写了简单的页面效果,做增删查改 这是我系统学习所得,希望能对你有所帮助 项目部署: 1;导包,就是web-inf下lib,让后把这些选中,单击右键build path -->add library 2;web.xml配置文件,这里几乎不需要改,以后要用只需要复制就行 3;具体框架配置文件,都集中在了sourceConfig文件夹下,这个自己琢磨,以后再用这个框架也是几乎不需要改的,之所以说几乎,是因为只需要改包名就可以了 4;写bean,写dao,写service,写controller,这是重点 5;bean下写要操作的数据表,记住这个类药与数据库中的表一致,最好一模一样,避免麻烦 6;dao下写数据库操作内容,那下面有两个文件名一样的,一个是java文件,一个是xml文件,java文件是定义方法名,xml文件是让你写具体的数据操作方法的,格式就是这样,你看看就能懂,你只需要这样写,这个框架就可以识别,吧你在xml中写的数据库操作方法匹配到java文件中的方法,这是mybatis做的事 7;service包中放你的业务逻辑层,具体来说就是,dao中只放数据操作,service引用数据操作,还可以有其他操作 8;controller类的格式你要用心点了,这个是控制器,控制请求的跳转,他和servlet的功能类似, 功能引导: 为了让你更方便了解这个项目,说一下流程 1.把项目部署到tomcat,启动服务, 2.在浏览器中输入http://localhost:8080/AscentSys/user/in.do 3.这个命令的意思是,访问这个项目地址user/in.do,然后这个请求就会发送到controller中,在controller中首先匹配到user,在匹配到in.do就调到具体的方法中去处理这个情求了,这里会跳转到users文件夹下的login.jsp页面中,解释一下,在spring-servlet.xml配置文件中有一句路径解析的,“前缀后缀”那个bean,意思是在返回的东西加前缀后缀,这里写的是啥你琢磨琢磨可以明白的 4.在login.jsp页面中,有个提交地址,是login.do,按上面说的,在controller中首先匹配到user,在匹配到login.do就调到具体的方法中去处理这个情求了,后面的流程我就不说了,自己看 5。再说一点,return的好多字符串,有的是redirect:/user/userlist.do这样的格式,意思是转发,就是转到另一个请求中去,同理,具体意思是,在controller中首先匹配到user,在匹配到userlist.do就调到下面的方法中去处理这个情求了, 6,关于传参数的问题,在表单中写的属性,在controller自动接收,也可以接受user对象,如果是对象,那个表单的格式你要看仔细了和一般表单的不同之处。琢磨琢磨你会明白的, 希望能对你有所帮助
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值