MyBatis两种操作数据库的结构

来源:互联网 发布:用网络看电视直播 编辑:程序博客网 时间:2024/04/30 13:00

昨天简单的写了Mybatis的实现,初步实现了操作数据库的功能,今天学习两种Mybatis框架的常用结构,实现了其功能,稍作整理,方便使用。

第一种结构:

1.Student类中为基本属性:注意,必须要有空参的构造方法,否则会报错。

2.StudentDaoI为借口,写操作数据库的抽象方法。(以插入和查询为例)

public interface StudentDaoI {
void insertStu(Student stu);
void updateStu(Student stu);
void deleteStu(int id);
List<Student> selectAll();
List<Student> selectByName(String name);
Student selectById(int id);
}

3.StudentDaoImpl类继承接口,方法的具体实现

public class StudentDaoImpl implements StudentDaoI{
private SqlSessionFactory sf;
public StudentDaoImpl() {
super();
}
public StudentDaoImpl(SqlSessionFactory sf) {
this.sf = sf;
}
@Override
public void insertStu(Student stu) {
// TODO Auto-generated method stub
SqlSession session = sf.openSession();
try {
session.insert("test.insert",stu);
session.commit();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
session.close();
}
}

@Override
public List<Student> selectAll() {
// TODO Auto-generated method stub
SqlSession session = sf.openSession();
List<Student> list=null;
try {
list = session.selectList("test.selectAll");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
session.close();
}
return list;
}

4.连接数据库的配置,这里应该放在前面。

<?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"></properties>
<typeAliases>
<package name="com.po" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
<mappers>
   <mapper resource="Student.xml"/>
</mappers>
</configuration>

5.配置student.xml映射文件,写sql语句(以插入和查询为例)

<?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="test">

   <select id="selectById" parameterType="int" resultType="student">
select * from student where id = #{id}
   </select>

   <insert id="insert" parameterType="student">
<selectKey keyProperty="id" order="AFTER" resultType="int">
select last_insert_id()
</selectKey>
   insert into student(name,gender,age,address)
   values(#{name},#{gender},#{age},#{address})
   </insert>

6.在测试类进行测试

public class StudentTest {
private SqlSessionFactory sf= null;
@Before
public void before(){
InputStream in=null;
try {
in = Resources.getResourceAsStream("mybatis-config.xml");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sf = new SqlSessionFactoryBuilder().build(in);
}
@Test
public void testselectAll() {
StudentDaoI dao = new StudentDaoImpl(sf);
List<Student> stu = dao.selectAll();
System.out.println(stu);
}
@Test
public void testselectById() {
StudentDaoI dao = new StudentDaoImpl(sf);
Student stu = dao.selectById(3);
System.out.println(stu);
}
@Test
public void testInsert(){
StudentDaoI dao = new StudentDaoImpl(sf);
Student stu = new Student("肚脐","男",23,"无锡");
dao.insertStu(stu);
System.out.println(stu);
}
}

第二种结构:


1.Student类与上面相同

2.StudentMapper为借口,写抽象方法,StudentMapper.xml进行配置,与上面基本类似,配置区别有:

指向映射文件<mapper namespace="com.dao.StudentMapper">

3.数据库连接配置,同上

写成package,会自动查找包内文件,减少代码量。


        <mappers>
<!-- <mapper resource="StudentMapper.xml" /> -->
     <package name="com.dao"/>
       </mappers>

4.测试类进行测试(查询,插入为例)

public class StudentTest {


private SqlSessionFactory sf;//工厂只要创建一回

@Before
public void before(){
InputStream in = null;
try {
in = Resources.getResourceAsStream("mybatis-config.xml");
} catch (IOException e) {
e.printStackTrace();
}
sf = new SqlSessionFactoryBuilder().build(in);
}


@Test
public void testSelectById() {
SqlSession session = sf.openSession(); //每次创建,因为它是线程不安全的
StudentMapper dao = session.getMapper(StudentMapper.class);
Student stu = dao.selectById(3);
System.out.println(stu);
session.close();
}

@Test
public void testInsert(){
SqlSession session = sf.openSession();
Student student = new Student("龟仙人", "男", 800, "花果山");
StudentMapper dao = session.getMapper(StudentMapper.class);
dao.insert(student);
session.commit();
System.out.println(student);
session.close();
}

@Test
public void testSelectAll() {
SqlSession session = sf.openSession(); //每次创建,因为它是线程不安全的
StudentMapper dao =session.getMapper(StudentMapper.class);
List<Student> stuLst = dao.selectAll();
System.out.println(stuLst);
session.close();
}

        Hibernate框架与MyBatis框架基本学完了,进行JAVA学习也2个多月了,之所以现在开始写博客,是因为博主感觉自己已经慢慢的开始入门了,对java也产生了一定的兴趣,曾经无数次的想放弃,感到自己不适合学习编程,到现在开始走向通往菜鸟程序员的路上,相信很多零基础的学习者们一定感同身受。java知识种类繁多,不断更新,现在开始记录下自己学习的点点滴滴,有助于理清思路,方便以后学习中查阅,博主希望有时间能够补充基础知识的复习,毕竟感觉基础还不是很牢固,最近有时间会先整理Hibernate相关表的创建关联的操作。

        博主在这里祝每一菜鸟程序员都能仕途顺利,博主也会向成为一个技术精湛的“手艺人”而努力。

1 0
原创粉丝点击