Mybatis简介(sqlsession)连接,操作数据库
1.MyBatis 本是apache的一个开源项目iBatis
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集
Mybatis其实就是对JDBC的封装
Orm(Object Relational Mapping)对象关系(关系型数据库)映射
2.xml方法使用步骤
总的xian项目结构(web.xml中不需要配置)
1.导包
2.创建mybatis配置文件(任意位置任意名称,但是建议放在src下且名称mybatis.xml)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://www.mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 设置一个默认的连接环境信息 -->
<environments default="development">
<!-- 连接环境信息,取一个任意唯一的名字 -->
<environment id="development">
<!-- mybatis使用jdbc事务管理方式 -->
<transactionManager type="JDBC"/>
<!-- mybatis使用连接池方式来获取连接 -->
<dataSource type="POOLED">
<!-- name=""(url、username)中的名字不可修改 -->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/studentdb?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 加载映射文件-->
<mappers>
<package name="com.oracle.mapper"></package>
</mappers>
</configuration>
3.新建bean包在此包中新建实体类
此处为Student类
4.新建包存放bean对应的接口,同时新建beanMappper.xml也放在此包中
beanMappper.xml实现接口中的方法,
StudentMapper.java中代码如下
package com.oracle.mapper;
import java.util.List;
import com.oracle.mybatis.bean.Student;
public interface StudentMapper {
//增加学生
public void addStudent(Student student);
//删除
public void delStudent(int sid);
//修改
public void updateStudent(Student student);
//根据条件查询
public Student selectStudent(String string, String string2);
//查询全部
public List<Student>selectallStudent(String string);
}
StudentMapper.xml中代码如下
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://www.mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace要和StudentMapper关联起来 -->
<mapper namespace="com.oracle.mapper.StudentMapper">
<select id="selectStudent" resultType="com.oracle.mybatis.bean.Student" >
select * from student where sname = #{0} and gender=#{1} ;
</select>
<select id="selectallStudent" resultType="com.oracle.mybatis.bean.Student" >
select * from student where gender=#{0}
</select>
<insert id="addStudent" parameterType="com.oracle.mybatis.bean.Student">
insert into student(sname,gender,cid) values(#{sname},#{gender},#{cid})
</insert>
<delete id="delStudent" parameterType="com.oracle.mybatis.bean.Student">
delete from student where sid=#{0}
</delete>
<update id="updateStudent" parameterType="com.oracle.mybatis.bean.Student">
update student set gender=#{gender} where sid=#{sid}
</update>
</mapper>
这里的id(如id="addStudent)要和上面接口中的方法名一致, <!-- namespace要和StudentMapper关联起来 -->
<mapper namespace="com.oracle.mapper.StudentMapper">
5.将接口和beanMappper.xml关联
6.新建测试类编写程序调用接口中的方法
package com.oracle.mybatis.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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 com.oracle.mapper.StudentMapper;
import com.oracle.mybatis.bean.Student;
public class Test {
public static void main(String[] args) throws IOException {
InputStream is=Resources.getResourceAsStream("MyBatis.xml");
SqlSessionFactory sessionfactory=new SqlSessionFactoryBuilder().build(is);
SqlSession sqlsession=sessionfactory.openSession();
StudentMapper studentMapper= sqlsession.getMapper(StudentMapper.class);
/*增加
* Student stu=new Student();
stu.setSname("手机");
stu.setGender("男");
stu.setCid(2);
studentMapper.addStudent(stu);*/
/*修改
* Student stu=new Student();
stu.setGender("女");
stu.setSid(13);
studentMapper.updateStudent(stu);*/
/*删除
* studentMapper.delStudent(13);*/
/*查找单个
Student stu=studentMapper.selectStudent("小红","女");
System.out.println(stu);*/
/*查询多个
* List<Student>list=studentMapper.selectallStudent("男");
for(Student stu:list){
System.out.println(stu);
}*/
sqlsession.commit();
sqlsession.close();
}
}
注意:1.bean中的属性名一定要和数据库一样
2.mapper.xml和beanMapper接口要放在同一个包中,名字要一致。
3.接口中的方法名和xml中的id要一致
4.mybatis.xml中要和com.oracle.mapper包联系起来
<!-- 加载映射文件-->
<mappers>
<package name="com.oracle.mapper"></package>
</mappers>