Import the development kit asm – 3.2.0. RELEASE. Jar asm – 3.3.1. Jar c3p0-0.9. Jar additional – 2.2.2. Jar com.springsource.net.sf.cglib-2.2.0.jar Com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.apache.commons.logging-1.1.1.jar Com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar Jackson – core – asl – 1.7.2. Jar Jackson – mapper – asl – 1.7.2. Jar Javassist 3.17.1 – GA. Jar javax.mail. Servlet, JSP, JSTL. Jar JSF – API. Jar JSF – impl. Jar JSTL – impl. Jar. Junit jar log4j – 1.2.17. Jar Mybatis – 3.2.2. Jar mybatis – spring – 1.2.0. Jar mysql connector – Java – 5.1.26 – bin. The jar Org. Hamcrest. Core_1. 1.0. V20090501071000. Jar org. Springframework. Transaction – 3.2.2. The jar slf4j – API – 1.7.5. Jar Slf4j – log4j12-1.7.5. Jar spring aop — 3.2.0. The jar spring – beans – 3.2.0. The jar spring – the context – 3.2.0. The jar Spring – the core – 3.2.0. RELEASE. Jar spring – expression – 3.2.0. The jar spring – JDBC – 3.2.0. The jar Spring – the orm – 3.2.0. RELEASE. Jar spring – test – 3.2.0. The jar spring – web – 3.2.0. The jar spring – webmvc – 3.2.0. The jar Integrate Spring and Mybatis related configuration file log4j.properties
Rules reminder: DEBUG < INFO < WARN < ERROR < FATAL
Global logging configuration log4j.rootLogger=debug,stdout
My logging configuration… log4j.logger.cn.jbit.mybatisdemo=DEBUG
Console output… log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n
log4j.logger.org.apache.ibatis=DEBUG log4j.logger.org.apache.jdbc.SimpleDataSource=DEBUG log4j.logger.org.apache.ibatis.jdbc.ScriptRunner=DEBUG log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapclientDelegate=DEBUG log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG
MyBatis – config. XML file
Mybatis will automatically scan the javabean under the package you specify, and set an alias by default. The default name is the unqualified class name to use as its alias.
Through package, you can directly specify the name of package, MyBatis will automatically scan the javabean under your specified package,Copy the code
An alias is set by default, and the default name is the unqualified class name to alias it. –>
Configure the data source, remember to remove the data from myBatis-config.xml
<tx:advice id=”advice” transaction-manager=”transactionManager”> tx:attributes <tx:method name=”insert*” propagation=”REQUIRED” rollback-for=”Exception” /> <tx:method name=”update*” propagation=”REQUIRED” rollback-for=”Exception” /> <tx:method name=”delete*” propagation=”REQUIRED” rollback-for=”Exception” /> <tx:method name=”*” propagation=”SUPPORTS” /> </tx:attributes> </tx:advice>
aop:config
<context:component-scan base-package=”cn.itcast”> <context:exclude-filter type=”annotation” expression=”org.springframework.stereotype.Controller” /> </context:component-scan>
Game: www.cungun.com
drop database if exists mybatis; create database mybatis CHARACTER SET UTF8; use mybatis;
create table dept(
dept_id int primary key auto_increment, dept_name varchar(50), dept_address varchar(50) );
Entity:
package zhongfucheng.entity;
import java.io.Serializable;
public class Dept implements Serializable {
private Integer deptId; private String deptName; private String deptAddress; public Integer getDeptId() { return deptId; } public void setDeptId(Integer deptId) { this.deptId = deptId; } public String getDeptName() { return deptName; } public void setDeptNamhttp: //wwww.cungun.com e(String deptName) {this.deptName = deptName; } public String getDeptAddress() { return deptAddress; } public void setDeptAddress(String deptAddress) { this.deptAddress = deptAddress; } @Override public String toString() { return “Dept [deptId=” + deptId + “, deptName=” + deptName + “, deptAddress=” + deptAddress + “]”; }}
The mapping file