IDEA使用MyBatis【超级详细,绝对能运行】
先上成功运行结果:
1.新建项目 ,这里新建一个普通的JavaSE项目,当然你也可以新建web项目
然后点Next,然后再点Finish就好了。
2.创建resources文件夹,该文件夹和java文件夹同一级
创建完以后,在文件夹上点右键:
3.在Java文件夹中新建学生实体类,对应数据库中的学生表:
package com.leo;
import lombok.Data;
@Data
public class Student {
/**
*
*/
private Integer id;
/**
*
*/
private String name;
/**
*
*/
private String sex;
/**
*
*/
private String specialty;
/**
*
*/
private String grade;
}
然后在类名上按Alt+Insert,会出来这个,没有的话应该是没装插件,先去装mybatis的插件:
然后在studentmapper.xml 中写sql语句:
<?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="com.leo.StudentMapper">
<!--auto generated Code-->
<resultMap id="BaseResultMap" type="com.leo.Student">
<result column="id" property="id" jdbcType="INTEGER"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="sex" property="sex" jdbcType="VARCHAR"/>
<result column="specialty" property="specialty" jdbcType="VARCHAR"/>
<result column="grade" property="grade" jdbcType="VARCHAR"/>
</resultMap>
<!--auto generated Code-->
<sql id="Base_Column_List">
id,
`name`,
sex,
specialty,
grade
</sql>
<!--auto generated Code-->
<insert id="insert" useGeneratedKeys="true" keyProperty="student.id">
INSERT INTO student (
id,
`name`,
sex,
specialty,
grade
) VALUES (
#{student.id,jdbcType=INTEGER},
#{student.name,jdbcType=VARCHAR},
#{student.sex,jdbcType=VARCHAR},
#{student.specialty,jdbcType=VARCHAR},
#{student.grade,jdbcType=VARCHAR}
)
</insert>
<!--auto generated Code-->
<insert id="insertSelective" useGeneratedKeys="true" keyProperty="student.id">
INSERT INTO student
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="student.id!=null">id,</if>
<if test="student.name!=null">`name`,</if>
<if test="student.sex!=null">sex,</if>
<if test="student.specialty!=null">specialty,</if>
<if test="student.grade!=null">grade,</if>
</trim>
VALUES
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="student.id!=null">#{student.id,jdbcType=INTEGER},
</if>
<if test="student.name!=null">#{student.name,jdbcType=VARCHAR},
</if>
<if test="student.sex!=null">#{student.sex,jdbcType=VARCHAR},
</if>
<if test="student.specialty!=null">#{student.specialty,jdbcType=VARCHAR},
</if>
<if test="student.grade!=null">#{student.grade,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<!--auto generated Code-->
<insert id="insertList">
INSERT INTO student (
id,
`name`,
sex,
specialty,
grade
)VALUES
<foreach collection="students" item="student" index="index" separator=",">
(
#{student.id,jdbcType=INTEGER},
#{student.name,jdbcType=VARCHAR},
#{student.sex,jdbcType=VARCHAR},
#{student.specialty,jdbcType=VARCHAR},
#{student.grade,jdbcType=VARCHAR}
)
</foreach>
</insert>
<!--auto generated Code-->
<update id="updateByPrimaryKeySelective">
UPDATE student
<set>
<if test="student.name != null">`name`= #{student.name,jdbcType=VARCHAR},</if>
<if test="student.sex != null">sex= #{student.sex,jdbcType=VARCHAR},</if>
<if test="student.specialty != null">specialty= #{student.specialty,jdbcType=VARCHAR},</if>
<if test="student.grade != null">grade= #{student.grade,jdbcType=VARCHAR}</if>
</set>
WHERE id = #{student.id,jdbcType=INTEGER}
</update>
<select id="selectOne" resultType="com.leo.Student">
select * from student where id = #{id};
</select>
</mapper>
然后新建xml文件,命名为config.xml,这是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>
<settings>
<!-- Globally enables or disables any caches configured in any mapper under this configuration -->
<setting name="cacheEnabled" value="false"/>
<!-- Sets the number of seconds the driver will wait for a response from the database -->
<setting name="defaultStatementTimeout" value="5"/>
<!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- Allows JDBC support for generated keys. A compatible driver is required.
This setting forces generated keys to be used if set to true,
as some drivers deny compatibility but still work -->
<setting name="useGeneratedKeys" value="true"/>
</settings>
<!-- Continue editing here -->
<!-- 数据库环境 -->
<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/stumanage?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- 映射文件 -->
<mappers>
<mapper resource="StudentMapper.xml"/>
</mappers>
</configuration>
最后写一个含有主函数的类测试一下:
public static void main(String[] args) {
// 根据 config.xml 配置的信息得到 sqlSessionFactory
String resource = "config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 然后根据 sqlSessionFactory 得到 session
SqlSession session = sqlSessionFactory.openSession();
// 最后通过 session 的 selectList() 方法调用 sql 语句 listStudent
Student student = session.selectOne("selectOne", 6);
System.out.println(student.toString());
}