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’s 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 JavaBeans you specify under the package, and set an alias name by default. The default name is the unqualified class name as its alias.

< TypeAliases >
–>

MyBatis will automatically scan the JavaBeans you specify under the package, and set an alias name by default. The default name is the unqualified class name as its alias. -->

<package name=”zhongfucheng.entity”/>

</typeAliases>

<! — <mappers>

    <mapper resource="cn/itcast/scm/entity/DeptMapper.xml" />
</mappers>
     -->

</configuration>

1.

applicationContext.xml

Configure the data source, Remember to remove the myBatis - config. XML data sources related configuration < bean id = "dataSource" class = "boPooledDataSource com.mchange.v2.c3p0.Com" > < property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/scm? useUnicode=true&amp; characterEncoding=UTF-8" /> <property name="user" value="root" /> <property name="password" value="root" /> </bean> <! - configuration session factory - > < bean id = "sqlSessionFactory" class = "org. Mybatis. Spring. SqlSessionFactoryBean" > < property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:myBatis-config.xml" /> <! Mybatis -config --> <! -- <property name="mapperLocations" value="classpath:zhongfucheng/dao/*.xml"/>--> </bean> <! > <bean id="transactionManager" --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <! > <tx:advice id="advice" transactionManager ="transactionManager"> <tx:attributes> <! -- By default, only runtime exceptions are handled -- bb0 <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> <aop:advisor advice-ref="advice" pointcut="execution(*) --> <aop:config> <aop:advisor advice-ref="advice" pointcut="execution(* cn.itcast.scm.service.impl.*.*(..) )" /> </aop:config> <! Configure sessionTemplate; Already encapsulates the cumbersome data operations - > < bean id = "sqlSessionTemplate" class = "org. Mybatis. Spring. SqlSessionTemplate" > < constructor - arg name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <! -- <context:component-scan base-package="*" /> --> <! -- automatic scan component, to remove the controller, they are configured in spring-mvc.xml, if not removed will affect transaction management. --> <context:component-scan base-package="cn.itcast"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <! If the name of the interface class under the package (including subpackages) set in BasePackage is consistent with the defined namespace in the mapper. XML file, it will be converted to Spring beans. At the point of the call it will be possible to inject an interface instance -> - in the @Autowired manner

<! —

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> &lt; ! &ndash; Be careful if you change the package name here. &gt; <property name="basePackage" value="zhongfucheng.dao" /> </bean>

–>

1 write entity and SQL mapping game file SQL script

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)

);

Insert into dept(dept_name,dept_address) values(‘ 1 ‘,’ 1 ‘); Insert into dept(dept_name,dept_address) values(‘ 1 ‘,’ 1 ‘); INSERT INTO DEPT (DEPT NAME, DEPT ADDRESS) VALUES (‘ Dept. name ‘,’ Dept. address ‘); select * from dept;

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 setDeptName(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

<mapper namespace=”zhongfucheng.entity.DeptMapper”>

<resultMap type="Dewww.cungun.compt" id="deptResultMap"> <id property="deptId" column="dept_id" /> <result property="deptName" column="dept_name" /> <result property="deptAddress" column="dept_address" /> </resultMap> <! -- id and namespace are used to locate the SQL statement, parameterType represents the type of the parameter, ResultMap return type -- bb0 <select id="selectDept" parameterType="Integer" resultMap="deptResultMap"> <! #{deptID} --> select * from dept where dept_id=#{deptID} </select> <insert id="insertDept" parameterType=" dept insert into dept(dept_name,dept_address) values(#{deptName},#{deptAddress}); </insert>

</mapper>