1、创建mybatis-config.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> <properties resource="db.properties"/> <typeAliases> <!-- 两种方法随便用,建议用第二种 --> <!--<typeAlias type="com.huawei.bean.Student" alias="Student"/>--> <package name="com.huawei.bean"/> </typeAliases> <environments default="development"> <environment id="development"> <!-- 配置事务 --> <transactionManager type="JDBC" /> <!-- 配置数据库连接信息 --> <dataSource type="POOLED"> <property name="driver" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/huawei/bean/Student.xml"/> </mappers> </configuration>
2、根据数据表创建相应的实体类
package com.huawei.bean; public class Student { private Integer id; private String name; private Integer grade; private String className; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getGrade() { return grade; } public void setGrade(Integer grade) { this.grade = grade; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } }
3、创建mapper文件完成对实体类的映射,该文件同样是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="com.huawei.mapper.StudentMapper"> <insert id="add" parameterType="Student"> insert into student(id,name,grade,classname) values(seq_student_id.nextval,#{name},#{grade},#{className}) </insert> <resultMap type="Student" id="student"> <result column="grade_id" property="grade"/> </resultMap> <select id="getStudents" resultMap="student"> select * from student </select> </mapper>
4、创建SQLSession,并且通过SqlSession完成对数据库的操作
package com.huawei.jutilTest; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import com.huawei.bean.Student; import com.huawei.mapper.StudentMapper; import com.huawei.util.MybatisUtil; public class JutilTest { @Test public void doAdd() { SqlSession session = null; try { session = MybatisUtil.createSession(); Student bean = new Student(); bean.setName("小明"); bean.setGrade(2); bean.setClassName("三年级"); //注意区别 session.getMapper(StudentMapper.class).add(bean); System.out.println("执行成功"); session.commit(); } catch (Exception e) { e.printStackTrace(); session.rollback(); }finally{ MybatisUtil.sessionClose(session); } } @Test public void getStudents(){ SqlSession session = null; List<Student>list =null; session = MybatisUtil.createSession(); //注意 list=session.getMapper(StudentMapper.class).getStudents(); for(int i=0;i<list.size();i++){ System.out.println(list.get(i).getName()+","+list.get(i).getGrade()); } MybatisUtil.sessionClose(session); } }
5、最佳实现
1)创建相应的MynbatisUtil完成对Session的获取
package com.huawei.util; 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; public class MybatisUtil { //初始化 private static SqlSessionFactory factory; static{ try { InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); factory = new SqlSessionFactoryBuilder().build(is); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //创建SQLSession public static SqlSession createSession(){ return factory.openSession(); } //关闭SQLSession public static void sessionClose(SqlSession session){ if(session!=null) session.close(); } }
2)注意参数名
3)使用mapper接口处理
package com.huawei.mapper; import java.util.List; import com.huawei.bean.Student; public interface StudentMapper { public void add(Student bean); public List<Student> getStudents(); }
项目结构
时间: 2024-11-08 19:14:43