准备工作,创建学生表,创建基础数据
(1)创建项目,搭建包结构
?
(2)导入mybatis相关jar包和mysql驱动包
?注意,如果你的mysql环境比较新,最好用版本高的connector-J。
(3)在src根下创建mybatis主配置文件mybatis-config.xml,搭建配置文件结构。
(4)创建mapper包结构,创建sql映射文件StudentMapper.xml
(5)创建学生类,注意,Student类中一定要有空构造函数
、
mybatis-config.xml文件
作用是连接数据库,以及注册sql映射文件StudentMapper.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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="1111"/>//连接数据库
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/mapper/StudentMapper.xml"/>//注册sql的映射文件
</mappers>
</configuration>
?StudentMapper.xml
解释一下,等会我们在测试类中调用这里的sql映射用的是
session.selectOne("sql.getById",1001);
这里的sql就是mapper文件namespace标签的值,getById就是select等标签的id,起到一个定位的作用。
执行sql语句返回的对象 resultType="com.domain.Student"
执行sql语句需要传递的对象 parameterType="java.lang.Integer"
?之后用到的Student student=session.selectOne("sql.getById",1001);,返回对象是Student,传递的对象是Integer
<?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="sql">
<select id="getById" resultType="com.domain.Student" parameterType="java.lang.Integer">
select * from student where id = #{id}
</select>
<select id="getAll" resultType="com.domain.Student" >
select * from student
</select>
<insert id="add" parameterType="com.domain.Student">
insert into student(id,name,time) values(#{id},#{name},#{time})
</insert>
<update id="update" parameterType="com.domain.Student">
update student set name=#{name},time=#{time} where id=#{id}
</update>
<delete id="reduce" parameterType="java.lang.Integer">
delete from student where id=#{id};
</delete>
</mapper>
测试方法
public static void main(String[] args) throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session=sqlSessionFactory.openSession();//这里的就是模板,都一样,可以封装
Student student=session.selectOne("sql.getById",1001);
System.out.println(student);//记住Student要重写toString方法
}
?输出成功
?多条记录用集合接收结果
public static void main(String[] args) throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session=sqlSessionFactory.openSession();
List<Student> list=session.selectList("sql.getAll");
for (Student s:list){
System.out.println(s);
}
}
输出结果
?注意,如果是更改数据库的操作,需要有session.commit();方法
public static void main(String[] args) throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session=sqlSessionFactory.openSession();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String date = df.format(new Date());// new Date()为获取当前系统时间,也可使用当前时间戳
Student student1=new Student(1005,"孙七",date);
session.insert("sql.add",student1);
session.commit();
}
插入结果
?最后记得关闭session
session.close();
|