StudentDao接口:
package cim.bjpowernode.dao;
import cim.bjpowernode.domain.Student;
public interface StudentDao {
//查询一个学生,方法
Student selectStudentById(Integer id);
//添加学生
//返回值int:表示本次操作影响数据库的行数
int insertStudent(Student student);
}
StudentDao.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="cim.bjpowernode.dao.StudentDao">
<!-- <select id="selectBlog" resultType="Blog">-->
<!-- select * from Blog where id = #{id}-->
<!-- </select>-->
<!-- 查询一个学生Student
<select>:表示查询操作,里面是select语句
id:要执行的sql语句的唯一标识,是一个自定的字符串
推荐使用dao接口中的方法名称
resultType:用来告诉告诉mybaits,执行sql语句,把数据赋值给那个类型的java对象
值现在使用java对象的全限定名称
cim.bjpowernode.domain.Student告诉 框架要转换的对象
当执行sql语句之后,把执行的结果转成student对象,由框架由反射机制创建student类的对象,把同名的列赋给同名的属性
#{student}:占位符,表示从java程序中传入过来的数据
-->
<select id="selectStudentById" resultType="cim.bjpowernode.domain.Student">
-- select id,name,email,age from student where id=1001
select id,name,email,age from student where id=#{studentId}
</select>
<!--添加insert-->
<!--
insert into student values (1003,"李峰","lifeng@qq.com",26)
如果传入一个java对象,使用#{属性名} 获取此属性的值
属性值放到#{}占位符的位置,mybaits执行此属性对应的getxxx()
-->
<insert id="insertStudent">
insert into student values (#{id},#{name},#{email},#{age})
</insert>
</mapper>
<!--
1.约束文件
http://mybatis.org/dtd/mybatis-3-mapper.dtd
作用:定义和限制当前文件中可以使用的标签和属性,以及标签的顺序
2.mapper跟标签
namespace="cim.bjpowernode.dao.StudentDao"命名空间,必须有值不能为空,唯一值,推荐使用接口的全限定名称
作用:参与识别sql语句的作用
3.在mapper里面可以写<insert>、<update>、<delete>、<selete>等标签
<insert>:里面是insert语句,表示执行的insert操作
<update>:里面是update语句
...
-->
MyTest类:
package cim.bjpowernode;
import cim.bjpowernode.domain.Student;
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 org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
public class MyTest {
//测试mybaits执行sql语句
@Test
public void testSelectStudentById() throws IOException {
//调用mybatis某个对象的方法,执行mapper文件中的sql语句
//mybatis核心类:SqlSessionFactory
//定义mybatis主配置文件的位置,从类路径开始的相对路径
String config="mybatis.xml";
//读取主配置文件,使用mybatis框架中的Resources类
InputStream inputStream = Resources.getResourceAsStream(config);
//3.创建SqlSessionFactory对象,使用SqlSessionFactoryBuidler类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//4.获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//5.指定要执行sql语句的id
//sql语句的id=namespace+“.”+<select>|update|insert|delete标签的id属性值
String sqlid="cim.bjpowernode.dao.StudentDao"+"."+"selectStudentById";
//6.通过SqlSession的方法,执行Sql语句 selectOne():有一个参数的方法
Student student=sqlSession.selectOne(sqlid);
System.out.println("使用mybatis要查询的学生:"+student);
//7.关闭SqlSession对象
sqlSession.close();
}
//在主xml文件添加显示日志,测试显示日志信息
@Test
public void testSelectStudentById2() throws IOException {
//调用mybatis某个对象的方法,执行mapper文件中的sql语句
//mybatis核心类:SqlSessionFactory
//定义mybatis主配置文件的位置,从类路径开始的相对路径
String config="mybatis.xml";
//读取主配置文件,使用mybatis框架中的Resources类
InputStream inputStream = Resources.getResourceAsStream(config);
//3.创建SqlSessionFactory对象,使用SqlSessionFactoryBuidler类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//4.获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//5.指定要执行sql语句的id
//sql语句的id=namespace+“.”+<select>|update|insert|delete标签的id属性值
String sqlid="cim.bjpowernode.dao.StudentDao"+"."+"selectStudentById";
//6.通过SqlSession的方法,执行Sql语句 selectOne()有两个参数
Student student=sqlSession.selectOne(sqlid,1002);
System.out.println("使用mybatis要查询的学生:"+student);
//7.关闭SqlSession对象
sqlSession.close();
}
//利用sql添加一个固定的学生信息
@Test
public void testInsertStudentById3() throws IOException {
String config="mybatis.xml";
//读取主配置文件,使用mybatis框架中的Resources类
InputStream inputStream = Resources.getResourceAsStream(config);
//3.创建SqlSessionFactory对象,使用SqlSessionFactoryBuidler类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//4.获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//5.指定要执行sql语句的id
//sql语句的id=namespace+“.”+select|update|insert|delete标签的id属性值
String sqlid="cim.bjpowernode.dao.StudentDao"+"."+"insertStudent";
//6.通过SqlSession的方法,执行Sql语句
int rows=sqlSession.insert(sqlid);
System.out.println("使用mybatis要添加一个的学生,rows="+rows);
//mybatis默认执行sql语句是 手工提交事务模式,在做insert,update,delete后需要提交事务
sqlSession.commit();
//7.关闭SqlSession对象
sqlSession.close();
}
//利用insert(sqlid,student):利用Student对象添加一个不固定学生信息
@Test
public void testInsertStudentById4() throws IOException {
String config="mybatis.xml";
//读取主配置文件,使用mybatis框架中的Resources类
InputStream inputStream = Resources.getResourceAsStream(config);
//3.创建SqlSessionFactory对象,使用SqlSessionFactoryBuidler类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//4.获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//5.指定要执行sql语句的id
//sql语句的id=namespace+“.”+select|update|insert|delete标签的id属性值
String sqlid="cim.bjpowernode.dao.StudentDao"+"."+"insertStudent";
//6.通过SqlSession的方法,执行Sql语句
Student student=new Student();
student.setId(1004);
student.setName("李思思");
student.setEmail("lisisi@qq.com");
student.setAge(22);
int rows=sqlSession.insert(sqlid,student);
System.out.println("使用mybatis要添加一个的学生,rows="+rows);
//mybatis默认执行sql语句是 手工提交事务模式,在做insert,update,delete后需要提交事务
sqlSession.commit();
//7.关闭SqlSession对象
sqlSession.close();
}
//实行自动提交
@Test
public void testAutoInsertStudentById5() throws IOException {
String config="mybatis.xml";
//读取主配置文件,使用mybatis框架中的Resources类
InputStream inputStream = Resources.getResourceAsStream(config);
//3.创建SqlSessionFactory对象,使用SqlSessionFactoryBuidler类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//4.获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//5.指定要执行sql语句的id
//sql语句的id=namespace+“.”+select|update|insert|delete标签的id属性值
String sqlid="cim.bjpowernode.dao.StudentDao"+"."+"insertStudent";
//6.通过SqlSession的方法,执行Sql语句
Student student=new Student();
student.setId(1005);
student.setName("李明明");
student.setEmail("limingming@qq.com");
student.setAge(23);
int rows=sqlSession.insert(sqlid,student);
System.out.println("使用mybatis要添加一个的学生,rows="+rows);
//7.关闭SqlSession对象
sqlSession.close();
}
}
修改主配置文件加入显示日志信息的代码:
<?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>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!-- 配置数据源:创建Connection对象,连接数据库 -->
<dataSource type="POOLED">
<!--driver:驱动的内容-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!--连接数据库的url-->
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&charaterEcoding=utf-8"/>
<!--用户名-->
<property name="username" value="root"/>
<!--密码-->
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 是指定其他mapper文件的位置:
其他mapper文件的目的是找到其他文件sql语句
-->
<mappers>
<!--
使用mapper的resource属性指定mapper文件的路径
这个路径是从target/classes路径开始的
使用注意:
resource=“mapper文件的路径,使用/分隔路径
一个mapper resource指定一个mapper文件,如果有多个需要一个个指定
-->
<mapper resource="cim/bjpowernode/dao/StudentDao.xml"/>
</mappers>
</configuration>
<!--
1.约束文件:http://mybatis.org/dtd/mybatis-3-config.dtd
2.
-->
第二个测试结果
第三个测试结果:
第四个结果: