第一步:导入jar包
jar包链接:https://pan.baidu.com/s/1yKAjRRYHCtgtjxo0Zss4lQ
第二步:创建两个Source Folder文件夹( resources和test)
第三步:创建实体类
第四步:创建dao层接口、实现类、mapper映射文件
1、Dao接口:
2、Dao的实现类:
3、UserMapper文件:
第五步:创建service层接口、实现类
Service接口
Service实现类
第六步:在resource文件夹中编写mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 类型别名 -->
<typeAliases>
<package name="com.bdqn.pojo"/>
</typeAliases>
</configuration>
第七步:在resource文件夹中编写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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.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">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- 获得sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 映射数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 映射mybatis核心配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 映射mapper文件 -->
<property name="mapperLocations">
<list>
<value>classpath:com/bdqn/dao/**/*.xml</value>
</list>
</property>
</bean>
<!-- 获得sqlSession -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<bean id="UserMapper" class="com.bdqn.dao.user.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
<bean id="UserService" class="com.bdqn.service.UserServiceImpl">
<property name="userMapper" ref="UserMapper"/>
</bean>
</beans>
第八步:在test文件夹中编写测试方法