> 文章列表 > Spring整合mybatis的练习

Spring整合mybatis的练习

Spring整合mybatis的练习

表全查的半注解

1.导入jar

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

 

 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;}@Overridepublic 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<Emp> selectAll();
}

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

//接口package com.gao.service;import com.gao.bean.Emp;import java.util.List;public interface EmpService {List<Emp> 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 {@AutowiredEmpDao empDao;@Overridepublic List<Emp> findAll() {return empDao.selectAll();}
}

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--扫描component及同名注解--><context:component-scan base-package="com.gao"/><!--加载外部配置文件--><context:property-placeholder location="jdbc.properties"/><!--1.配置spring管理链接数据库的数据源--><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean><!--2.使用上面的数据源配置mybatis的SqlSessionFactory对象交给IOC容器管理,有了SqlSessionFactory对象就可以创建Session对象,有了SqlSession对象就有了代理对象,创建什么代理对象?有下面进行定义--><bean id="factoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"><!--引用数据源--><property name="dataSource" ref="dataSource"/><!--加载mybatis的核心配置文件--><property name="configLocation" value="mybatis.xml"/><!--给实体类起别名--><property name="typeAliasesPackage" value="com.gao.bean"/></bean><!--3.配置mapper扫描器,确定需要创建的代理对象--><bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.gao.dao"/></bean>
</beans>

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

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><settings><setting name="logImpl" value="log4j"/></settings>
</configuration>

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=rootlog4j.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;@Testpublic void test01(){ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");empService=context.getBean(EmpService.class);List<Emp> empList=empService.findAll();for (Emp emp : empList) {System.out.println(emp);}}
}

6.运行结果

 

 全注解

1.导入jar包

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

 

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;}@Overridepublic 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<Emp> selectAll();
}

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

//接口package com.gao.service;import com.gao.bean.Emp;import java.util.List;public interface EmpService {List<Emp> 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 {@AutowiredEmpDao empDao;public void setEmpDao(EmpDao empDao) {this.empDao = empDao;}@Overridepublic List<Emp> 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;@Testpublic void test01(){AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(SpringConfig.class);empService=context.getBean(EmpService.class);List<Emp> empList=empService.findAll();for (Emp emp : empList) {System.out.println(emp);}}
}

7.运行结果