使用Spring简化Mybatis
-
首先导入需要的spring的jar包与mybatis的 jar包,spring整合mybatis的包。
-
在src下创建applicationContext.xml文件并配置。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 封装数据库 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDirver"></property>
<property name="url" value="jdbc:sqlserver://localhost:8080;DatabaseName=Student"></property>
<property name="username" value="sa"></property>
<property name="password" value="123456"></property>
</bean>
<!-- 创建sqlsessionFacotry对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接信息来源于dataSource -->
<property name="dataSoure" ref="factory"></property>
</bean>
<!-- 扫描器相当于mybatis.xml中 mappers下package标签,扫描com.bjsxt.mapper包后会给对应接口创建对象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 要扫描的包 -->
<property name="basePackage" value="mapper"></property>
<!-- 与factory的关系 -->
<property name="sqlSessionFactory" ref="factory"></property>
</bean>
<!-- 使用sprig管理service的实现类 -->
<bean id="StudentService" class="service.StudentMapperImpl">
<property name="StudentMapper" ref="StudentMapper"></property>
</bean>
</beans>
- 在src下创建pojo包,并创建需要的实现类。
package pojo;
public class Student {
private int snum;
private String sname;
private int sex;
private int sclass;
public int getSnum() {
return snum;
}
public void setSnum(int snum) {
this.snum = snum;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getSclass() {
return sclass;
}
public void setSclass(int sclass) {
this.sclass = sclass;
}
@Override
public String toString() {
return "Student [snum=" + snum + ", sname=" + sname + ", sex=" + sex
+ ", sclass=" + sclass + "]";
}
}
- 在src下创mapper包,并创建类名+Mapper的一个接口,接口中是需要的方法。
package mapper;
import java.util.List;
import org.apache.ibatis.annotations.Select;
import pojo.Student;
public interface StudentMapper {
@Select("select * from student")
List<Student> sellAll();
}
- 在src下创建service包,创建类名+service的接口。
package service;
import java.util.List;
import pojo.Student;
public interface StudentService {
List<Student> show();
}
并创建接口的实现类,实现接口中的方法:
package service;
import java.util.List;
import mapper.StudentMapper;
import pojo.Student;
public class StudentServiceImpl implements StudentService {
private StudentMapper studentmapper;
public StudentMapper getStudentapper() {
return studentmapper;
}
public void setStudentapper(StudentMapper studentmapper) {
this.studentmapper = studentmapper;
}
public List<Student> show(){
return studentmapper.sellAll();
}
}
- 在src下创建servlet包,并创建servlet:
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import service.StudentService;
import service.StudentServiceImpl;
public class sellServlet extends HttpServlet {
private StudentService studentservice;
public void init(){
//spring和web整合后所有信息都存放在webApplicationContext
ApplicationContext ac=WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
studentservice=ac.getBean("studentservice",StudentServiceImpl.class);
}
public void service(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException{
req.setAttribute("list", studentservice.show());
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
}
- 在src下创建测试类text:
package Test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.Student;
import service.StudentServiceImpl;
public class Test {
public static void main(String[] args) {
//ClassPathXmlApplicationContext 默认去classes文件夹根目录开始寻找
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
StudentServiceImpl bean=ac.getBean("studentservice",StudentServiceImpl.class);
List<Student> list=bean.show();
System.out.print(list);
}
}
- 在webRoot下的web-inf文件夹下创建web.xml文件并配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- 上下文参数 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- spring配置文件 -->
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 封装了一个监听器,帮助加载Spring的配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
- 最后在index.jsp页面上将数据库中的数据显示出来。
- 注:
编写 mapper 包下时必须使用接口绑定方案或注解方案(必须 有接口)。
需要在 Service 实现类中声明 Mapper 接口对象,并生成 get/set 方法。