Spring整合mybatis的练习

表全查的半注解

1.导入jar

Spring整合mybatis的练习_第1张图片

2.在src中创建bean,dao,service以及servlet包

Spring整合mybatis的练习_第2张图片 

 3.创建并书写实体类

package com.gao.bean;

public class Emp {
    private Integer eid;
    private String ename;
    private String dept;
    private String job;
    private Integer sal;
    private String phone;
    private String address;

    public Integer getEid() {
        return eid;
    }

    public void setEid(Integer eid) {
        this.eid = eid;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public String getDept() {
        return dept;
    }

    public void setDept(String dept) {
        this.dept = dept;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public Integer getSal() {
        return sal;
    }

    public void setSal(Integer sal) {
        this.sal = sal;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "eid=" + eid +
                ", ename='" + ename + '\'' +
                ", dept='" + dept + '\'' +
                ", job='" + job + '\'' +
                ", sal=" + sal +
                ", phone='" + phone + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

2.创建并书写EmpDao接口

package com.gao.dao;

import com.gao.bean.Emp;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Repository
public interface EmpDao {
    //全查
    @Select("select * from emp")
    List selectAll();
}

3.创建并书写EmpService接口及其接口类

//接口

package com.gao.service;

import com.gao.bean.Emp;

import java.util.List;

public interface EmpService {
    List findAll();
}


//实现类

package com.gao.service.impl;

import com.gao.bean.Emp;
import com.gao.dao.EmpDao;
import com.gao.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    EmpDao empDao;
    @Override
    public List findAll() {
        return empDao.selectAll();
    }
}

4..在src中创建配置Spring的配置文件




    
    
   
    
    
 
    
        
        
        
        
    

    
    
        
        
        
        
        
        
    

    
    
        
    



5.在src中创建mybatis的配置文件




        
            
        

6.在src中创建配置外部文件jdbc.properties及log4j.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mydb
jdbc.username=root
jdbc.password=root


log4j.properties略


7.在servlet包中创建测试类TestServlet

package com.gao.servlet;

import com.gao.bean.Emp;
import com.gao.service.EmpService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class ServletTest {
    EmpService empService;

    @Test
    public void test01(){
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
        empService=context.getBean(EmpService.class);

        List empList=empService.findAll();
        for (Emp emp : empList) {
            System.out.println(emp);
        }
    }
}

6.运行结果

Spring整合mybatis的练习_第3张图片

 

 全注解

1.导入jar包

Spring整合mybatis的练习_第4张图片

2在src中创建bean,dao,config,service,servlet包

Spring整合mybatis的练习_第5张图片 

3.在emp包中创建并书写实体类

package com.gao.bean;

public class Emp {
    private Integer eid;
    private String ename;
    private String dept;
    private String job;
    private Integer sal;
    private String phone;
    private String address;

    public Integer getEid() {
        return eid;
    }

    public void setEid(Integer eid) {
        this.eid = eid;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public String getDept() {
        return dept;
    }

    public void setDept(String dept) {
        this.dept = dept;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public Integer getSal() {
        return sal;
    }

    public void setSal(Integer sal) {
        this.sal = sal;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "eid=" + eid +
                ", ename='" + ename + '\'' +
                ", dept='" + dept + '\'' +
                ", job='" + job + '\'' +
                ", sal=" + sal +
                ", phone='" + phone + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

 4.在dao包中创建并书写EmpDao接口

package com.gao.dao;

import com.gao.bean.Emp;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface EmpDao {
    //全查
    @Select("select * from emp")
    List selectAll();
}

5.在service类创建并书写service接口及其实现类

//接口

package com.gao.service;

import com.gao.bean.Emp;

import java.util.List;

public interface EmpService {
    List findAll();
}


//实现类

package com.gao.service.impl;

import com.gao.bean.Emp;
import com.gao.dao.EmpDao;
import com.gao.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    EmpDao empDao;

    public void setEmpDao(EmpDao empDao) {
        this.empDao = empDao;
    }

    @Override
    public List findAll() {
        return empDao.selectAll();
    }
}

6.在servlet包中创建测试类TestServlet

package com.gao.servlet;

import com.gao.bean.Emp;
import com.gao.config.SpringConfig;
import com.gao.service.EmpService;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class TestServlet {
    EmpService empService;

    @Test
    public void test01(){
        AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(SpringConfig.class);
        empService=context.getBean(EmpService.class);

        List empList=empService.findAll();
        for (Emp emp : empList) {
            System.out.println(emp);
        }
    }
}

7.运行结果

Spring整合mybatis的练习_第6张图片

 

你可能感兴趣的:(mybatis,spring,java)