下载驱动jar包,以及mybaties包。在pom文件中添加约束即可
<!-- ********mybatis********* -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!-- ********mybatis********* -->
<!-- ********mysql驱动包********* -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<!-- ********mysql驱动包********* -->
添加配置文件
<?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>
<!-- 注册属性文件 -->
<properties resource="jdbc.properties" /> -----config.properties
<!-- 配置mybatis运行环境 -->
<environments default="development">
<!-- 配置开发环境 -->
<environment id="development">
<!-- jdbc事务管理器 -->
<transactionManager type="JDBC"/>
<!-- 数据源
UNPOOLED 不适用连接池 即 每次请求都会为其创建一个DB连接,适用完毕后,会马上将连接关闭
POOLED 数据库连接池来维护连接
JNDI 数据源可以定义到应用的外部,通过JDNI容器来获取数据库连接
-->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClass}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
<!-- 可以多个环境切换。配置上线环境 -->
<environment id="online">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClass}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!-- 映射器 -->
<mappers>
<!-- 注册映射文件 -->
<mapper resource="mapper.xml"/>
<mapper resource="mapper2.xml"/>
</mappers>
</configuration>
添加映射文件
<?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="reyco">
<insert id="insertStu" >
insert into student(id,name,score,hobby)
values(#{id},#{name},#{score},#{hobby})
<selectKey resultType="int" keyProperty="id" order="AFTER">
select last_insert_id();
</selectKey>
</insert>
<delete id="deleteStu" >
delete from student where id = #{id}
</delete>
<update id="updateStu">
update student set
name = #{name}, score = #{score},hobby= #{hobby}
where id = #{id}
</update>
<select id="selectStu" resultType="com.evecom.common.Student">
select * from student
</select>
<select id="selectStuById" resultType="com.evecom.common.Student" >
select * from student where id = #{id}
</select>
<select id="selectStuSlur" resultType="com.evecom.common.Student">
select * from student where name like '%' #{name} '%'
</select>
</mapper>