Mybatis_3

1、建项目

2、导入jar包

3、创建需要的包:更改了包名:com.cn.hm

 

4、在dao层删除impl,添加StudentMapper.xml,删除mapper包

(1)Studentdao接口中方法的名字要与Mapper映射文件中的id的名字必须相同;

(2)dao中有几个抽象方法,在mapper映射文件中就对应几套标签。一套标签对应一个方法。

(3)对于查询来讲,传递的参数类型和返回值类型必须跟接口中的参数类型和返回值类型保持一致。

5、业务层

(1)StudentServiceImpl

public class StudentServiceImpl implements StudentService {
//StudentDao层的反射类型对象,语法约定
    private StudentDao studentDao = SqlSessionUtil.getSession().getMapper(StudentDao.class);

    @Override
    public Student getById(String id) {
        Student student = studentDao.getById(id);

        return student;
    }

    @Override
    public void save(Student student) {
        studentDao.save(student);
    }

}

6、test测试

(1)测试根据id查询单条操作

public class Test1 {

    public static void main(String[] args) {

        // StudentService studentService = new StudentServiceImpl();

        StudentService studentService = (StudentService) ServiceFactory.getService(new StudentServiceImpl());

        //测试添加操作

       /* Student student = new Student();

        student.setId("A0006");

        student.setName("cxk");

        student.setAge(26);

        studentService.save(student);*/



        //测试根据id查询单条操作

        Student student = studentService.getById("A0002");

        System.out.println(student);



    }



}

 

(2)查询所有记录

public class Test1 {

    public static void main(String[] args) {

        // StudentService studentService = new StudentServiceImpl();

        StudentService studentService = (StudentService) ServiceFactory.getService(new StudentServiceImpl());

        //测试添加操作

       /* Student student = new Student();

        student.setId("A0006");

        student.setName("cxk");

        student.setAge(26);

        studentService.save(student);*/



        //测试根据id查询单条操作

        /*Student student = studentService.getById("A0002");

        System.out.println(student);*/



        //查询所有记录

       List<Student> studentList =  studentService.getAll();//控制层不干活,业务层拿数据

       for (Student student : studentList){

           System.out.println(student);

       }



    }



}

7、在业务层创建getAll()

(1)逆向生成——在StudentService接口中生成getAll();(双击StudentService,连敲击两下shift,找到实现类)

public interface StudentService {



    public Student getById(String id);



    public void save(Student student);



    List<Student> getAll();

}

(2)在StudentServiceImpl实现类重写方法getAll()

public class StudentServiceImpl implements StudentService {

    //StudentDao层的反射类型对象,语法约定

    private StudentDao studentDao = SqlSessionUtil.getSession().getMapper(StudentDao.class);



    @Override

    public Student getById(String id) {

        Student student = studentDao.getById(id);



        return student;

    }



    @Override

    public void save(Student student) {

        studentDao.save(student);

    }



    @Override

    public List<Student> getAll() {

       List<Student> studentList = studentDao.getAll();

        return studentList;

    }

}

8、开发dao层

(1)在StudentDao接口中生成getAll();(双击StudentDao,连敲击两下shift,找到xml文件)

public interface StudentDao {



    public Student getById(String id);



    public void save(Student student);



    List<Student> getAll();

}

(2)在StudentMapper.xml文件中写sql语句

<mapper namespace="com.cn.hm.dao.StudentDao">



    <select id="getAll" resultType="com.cn.hm.domain.Student">

        select * from tbl_student

    </select>

    <select id="getById" parameterType="java.lang.String" resultType="com.cn.hm.domain.Student">

        select * from tbl_student where id = #{id}

    </select>



    <insert id="save">

        insert into tbl_student(id,name,age) values(#{id},#{name},#{age})

    </insert>

</mapper>

源码:https://github.com/xiaoyaoltian/MyBatis_3 

学习交流用,不喜勿喷!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值