jar包
ant-1.9.6.jar
ant-launcher-1.9.6.jar
aopalliance-1.0.jar
asm-5.2.jar
aspectjweaver-1.8.13.jar
cglib-3.2.5.jar
commons-dbcp2-2.2.0.jar
commons-logging-1.2.jar
commons-pool2-2.5.0.jar
javassist-3.22.0-CR2.jar
log4j-1.2.17.jar
log4j-api-2.3.jar
log4j-core-2.3.jar
mybatis-3.4.5.jar
mybatis-spring-1.3.1.jar
mysql-connector-java-8.0.21.jar
ognl-3.1.15.jar
slf4j-api-1.7.25.jar
slf4j-log4j12-1.7.25.jar
spring-aop-5.0.2.RELEASE.jar
spring-aspects-5.0.2.RELEASE.jar
spring-beans-5.0.2.RELEASE.jar
spring-context-5.0.2.RELEASE.jar
spring-core-5.0.2.RELEASE.jar
spring-expression-5.0.2.RELEASE.jar
spring-jdbc-5.0.2.RELEASE.jar
spring-tx-5.0.2.RELEASE.jar
文件结构
1.持久化类
package po;
public class MyUser {
private Integer uid;
private String uname;
private String usex;
//get set.....
@Override
public String toString() {
return "MyUser [uid=" + uid + ", uname=" + uname + ", usex=" + usex + "]";
}
}
2.数据接口类
package dao;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import po.MyUser;
import java.util.List;
@Repository()
@Mapper
public interface UserDao {
public MyUser selectUserById(Integer uid);
public List<MyUser> selectAllUser();
public int addUser(MyUser u);
public int updateUser(MyUser u);
public int deleteUser(Integer uid);
public int deleteAll();
}
配置文件
1.mapper文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.UserDao">
<select id="selectUserById" parameterType="Integer" resultType="po.MyUser">
select * from user where uid = #{uid}
</select>
<select id="selectAllUser" resultType="po.MyUser">
select * from user
</select>
<insert id="addUser" parameterType="po.MyUser">
insert into user (uname,usex) values(#{uname},#{usex})
</insert>
<update id="updateUser" parameterType="po.MyUser">
update user set uname=#{uname},usex=#{usex} where uid=#{uid}
</update>
<delete id="deleteUser" parameterType="Integer">
delete from user where uid=#{uid}
</delete>
<delete id="deleteAll">
delete from user
</delete>
</mapper>
注意namespace是数据接口类的位置
2.mybatis-config
<?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>
<mappers>
<mapper resource="mybatis/UserMapper.xml" />
</mappers>
</configuration>
指定mapper文件的位置即可
若使用单纯的mybatis 则需要配置数据库的相关信息 同时使用的时候通过SqlSession去操作
单纯使用mybatis代码如下:
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/gustuy?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/UserMapper.xml" />
</mappers>
</configuration>
//1 读取操作文件
InputStream config=Resources.getResourceAsStream("mybatis-config.xml");
//2 构建SqlSessionFactory
SqlSessionFactory ssf=new SqlSessionFactoryBuilder().build(config);
//3 构建会话对象
SqlSession ss= ssf.openSession();
3.applicationContext
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="dao"></context:component-scan>
<context:component-scan base-package="controller"></context:component-scan>
<bean id="myDataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/gustuy?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- 事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="myDataSource"></property>
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="txManager"/>
<!-- 配置MyBatis工厂,同时指定数据源 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
</bean>
<!-- 自动装配mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
配置完成之后,UserDao接口会与mapper文件绑定实现,使用时直接构建UserDao对象使用其中的函数即可。注意UserDao接口中的函数名要和mapper文件中的id名相同。