使用Spring简化Mybatis

使用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 方法。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值