1.准备数据库: user
2.准备各层:
2.1 dao : UserMapper.java, UserMapper.xml (前者为接口,两个名字须一样): UserMapper.xml 代替了实现类
2.2 entity: User.java
2.3 service: UserService.java
2.4 service.impl: UserServiceImpl.java
2.5 配置jar包(16个)
2.6 准备文件:applicationContext.xml, log4j.properties, mybatis-config.xml
3.结合应用:
mybatis-config.xml *
<configuration>
<!--类型别名 -->
<typeAliases>
<package name="com.bdqn.entity" />
</typeAliases>
</configuration>
applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<!-- <property name="url">
<value><![CDATA[jdbc:mysql://127.0.0.1:3306/smbms?
useUnicode=true&characterEncoding=utf-8]]></value>
</property> -->
<property name="url" value="jdbc:mysql://127.0.0.1:3306/user?
useUnicode=true&characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- 配置SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 引用数据源组件 -->
<property name="dataSource" ref="dataSource" />
<!-- 引用MyBatis配置文件中的配置 -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!-- 配置SQL映射文件信息 -->
<!-- <property name="mapperLocations">
<list>
<value>classpath:cn/smbms/dao/**/*.xml</value>
</list>
</property> -->
</bean>
<!-- 配置DAO -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.bdqn.dao.UserMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- 配置业务Bean -->
<bean id="userService" class="com.bdqn.service.impl.UserServiceImpl">
<property name="userMapper" ref="userMapper" />
</bean>
</beans>
UserMapper.xml *
<mapper namespace="com.bdqn.dao.UserMapper">
<select id="findById" parameterType="User" resultType="User">
select * from user where uname=#{uname} and pwd=#{pwd}
</select>
<insert id="add" parameterType="User" >
insert into user values(#{uid},#{uname},#{pwd})
</insert>
<update id="update" parameterType="User">
update user set uname=#{uname},pwd=#{pwd} where uid=#{uid}
</update>
<delete id="delete" parameterType="int">
delete from user where uid=#{uid}
</delete>
<!-- 模糊查询 -->
<select id="selectByName" parameterType="String" resultType="User">
select * from user where uname like concat('%',#{uname},'%')
</select>
</mapper>
"UserServiceImpl.java"
public class UserServiceImpl implements UserService{
private UserMapper userMapper; //需要get, set 方法
public UserMapper getUserMapper() {
return userMapper;
}
public void setUserMapper(UserMapper userMapper) {
xxxxxx
接口:“UserMapper.java”
public interface UserMapper {
List<User> findById(User user);
int add(User user);
int update(User user);
int delete(int id);
List<User> selectByName(String name);
}