昨晚看了那么久了博客了,今天准备自己写一个demo试试。
1、新建一个数据库作为测试练手
2、在eclipse中新建一个java项目
3、开始写
- 先在在config文件夹下建mybatis_config.xml,配置数据库连接信息。
需要注意一个坑,第一行开头这段代码<?xml version="1.0" encoding="UTF-8"?>
,左边不能有空格
<?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>
<!-- 配置默认开发环境,default中的值与下面的开发环境id对应 -->
<environments default="development">
<!-- 配置开发环境 -->
<environment id="development">
<!-- JDBC事务 -->
<transactionManager type="JDBC"/>
<!-- 连接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="aaa"/>
<property name="password" value="123"/>
</dataSource>
</environment>
</environments>
</configuration>
- 根据数据库表写相应的实体类
注意:实体类的属性名一定要和数据库表的字段名相同,不然在后面的mapper中会抛很多让初学者怀疑人生的异常
package demo.cyj.pojo;
public class Student {
private int id ;
private String stu_name ;
private int stu_age ;
public Student() {}
public Student(int id, String stu_name, int stu_age) {
super();
this.id = id;
this.stu_name = stu_name;
this.stu_age = stu_age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStu_name() {
return stu_name;
}
public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
public int getStu_age() {
return stu_age;
}
public void setStu_age(int stu_age) {
this.stu_age = stu_age;
}
@Override
public String toString() {
return "Student [id=" + id + ", stu_name=" + stu_name + ", stu_age=" + stu_age + "]";
}
}
- 在mapper文件夹下建StudentMapper.xml,并做配置
<?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="demo.cyj.mapper.StudentMapper">
<select id="selectStuById" parameterType="int" resultType="demo.cyj.pojo.Student">
select * from student where id = #{id}
</select>
<update id="updateStuById" parameterType="demo.cyj.pojo.Student">
update student set stu_name=#{stu_name},stu_age=#{stu_age} where id=#{id}
</update>
<insert id="insertStu" parameterType="demo.cyj.pojo.Student">
insert into student(stu_name,stu_age) values(#{stu_name},#{stu_age})
</insert>
<delete id="delStuById" parameterType="int">
delete from student where id = #{id}
</delete>
<delete id="delStuByStu" parameterType="demo.cyj.pojo.Student">
delete from student where id = #{id}
</delete>
</mapper>
此时,可以向第一步中的mybatis_config.xml填加
<mappers>
<mapper resource="demo/cyj/mapper/StudentMapper.xml"/>
</mappers>
具体什么样子如下:
<?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>
<!-- 配置默认开发环境,default中的值与下面的开发环境id对应 -->
<environments default="development">
<!-- 配置开发环境 -->
<environment id="development">
<!-- JDBC事务 -->
<transactionManager type="JDBC"/>
<!-- 连接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="aaa"/>
<property name="password" value="123"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="demo/cyj/mapper/StudentMapper.xml"/>
</mappers>
</configuration>
- 最后就是写个测试类来测试了
package demo.cyj;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import demo.cyj.pojo.Student;
public class MybatisTest {
public static void main(String[] args) throws IOException {
//mybatis的配置文件的路径
String resource = "demo/cyj/config/mybatis_config.xml";
//读取配置文件的路径
InputStream inputStream = Resources.getResourceAsStream(resource);
//获取到sqlSession的构造工厂类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//括号内加true,那么会自动提交,否则(括号内不传参)最后要通过session.commit()手动提交
SqlSession session = sqlSessionFactory.openSession(true);
//查询,传递参数为("mapper名空间.方法名",参数)
Student s1 = session.selectOne("demo.cyj.mapper.StudentMapper.selectStuById", 1);
System.out.println("select:"+s1);
//修改
Student s2 = new Student(1,"cyj",19);//修改的内容存到对象中
int i2 = session.update("demo.cyj.mapper.StudentMapper.updateStuById",s2);
System.out.println("update:"+i2);
/*session.commit();*/
//插入
Student s3 = new Student(2,"ccc",20);
int i3 = session.insert("demo.cyj.mapper.StudentMapper.insertStu",s3);
System.out.println("insert:"+i3);
/*session.commit();*/
//删除byId
int i4 = session.delete("demo.cyj.mapper.StudentMapper.delStuById",3);
System.out.println("delStuById:"+i4);
/*session.commit();*/
//删除byStudent
Student s5 = new Student(4,"",0);
int i5 = session.delete("demo.cyj.mapper.StudentMapper.delStuByStu",s5);
System.out.println("delStuByStu:"+i5);
/*session.commit();*/
}
}
运行结果如下:
总结一下
- 要注意mysql版本和mysql-connector的版本问题
- 这里面的文件配置路径都是用 “/”来分割的
- mapper中的namespace可以随意写,只要在用的时候与之对应即可,但是一般要求代码规范,往往写的是当前mapper所在的路径
- mapper中的namespace,id会与SqlSession中的方法参数对应,resultType是select特有的
- 先将
mybatis_config.xml
的文件路径通过Resources.getResourceAsStream("路径")
放到InputStream中 - 再通过
SqlSessionFactoryBuilder().build(inputStream)
获取工厂类SqlSessionFactory对象 - 再通过
sqlSessionFactory.openSession()
从工厂中获取SqlSession对象
- 先将
- 映射顺序是:
- 先通过io流读取指定路径下的
mybatis_config.xml
文件,获取相关配置信息(如数据库连接配置,mappers配置) - 再从SqlSession操作(如
session.selectOne("StudentMapper.selectStuById", 1)
)找到对应名空间的mapper和对应的操作id,传入参数1,得到返回值