导入spring,mybaits,springmvc相关的jar包还有log4j,slf4j,mysql-connector...
create database if not exists sms;
use sms;
create table test_user(
id int(5) auto_increment,
name varchar(12),
age varchar(10),
primary key (id)
)ENGINE=Innodb default charset=utf8;
目录结构:-->com.x.controller
com.x.mapper
com.x.model
com.x.service
com.x.service.impl
com.x.test
config
工作流程-->model---User
public User{
private int id;
private String name;
private String age;
public User(){super();}
public User(int id,String name,String age){this.id=id;this.name=name;this.age=age;}
public void setId(int id){this.id=id;}
public int getId(){return id;}....
}
mapper---Dao
public interface UserMapper{
void addUser(User user);
boolean deleteUser(int id);
boolean updateUser(User user);
User findById(int id);
List<User> query();
}
mapperImpl---daoImpl---UserMapper.xml
<mapper namespace="com.x.model.UserMapper">
<select id="findById" parameterType="int" resultType="User">
select name,age from test_user where id=#{id}
</select>
<select id="query" resultType="User">
select name,age from test_user
</select>
<insert id="addUser" parameterType="User">
insert into test_user(name,age) values(#{name},#{age});
</insert>
<delete id="deleteUser" parameterType="int">
delete from test_user where id=#{id}
</delete>
<update id="udpateUser" parameterType="User">
update test_user set name=#{name},age=#{age} where id=#{id}
</update>
mybatis,spring整合核心配置文件--->
<bean id="dataSource" class="...dataSource.Drivermanagerdatasource">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/sms"/>
....
</bean>
<bean id="sqlSessionFactory" class="...sqlSessionFactorybean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:config/mybatis-config.xml"/>
</bean>
<bean class="..mapperScannerConfigurer">
<property name="basepath" value="com.x.UserMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<bean id="txManager" class="...datasourceTransactionManager">
<property name="datasource" value="datasource"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
mybatis-config.xml--->
<configuration>
<typeAliases>
<typeAlias alias="User" type="com.x.model.User"/>
</typeAliases>
<mappers>
<mapper resource="com/x/mapper/UserMapper.xml"/>
</mappers>
</configuration>
测试成功