Spring&Mybatis 整合开发基础

发布于:2021-09-29 00:27:43

首先新建MavenWeb项目,在webapp目录下新建WEB-INF目录,在其中新建web.xml文件:



xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
SpringDemo

index.html
index.htm
index.jsp
default.html
default.htm
default.jsp



导入相关jar包:



4.0.0
com.wu.springdemo
SpringDemo
0.0.1-SNAPSHOT
war



org.mybatis
mybatis
3.5.2



org.springframework
spring-core
5.2.9.RELEASE



org.springframework
spring-context
5.2.9.RELEASE



org.springframework
spring-beans
5.2.9.RELEASE



junit
junit
4.12




新建pojo,Student.java类:


package com.wu.pojo;

public class Student {
private String name;
private long id;
public Student() {}
public Student(String name,long id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Override
public String toString() {
return "Student [name=" + name + ", id=" + id + "]";
}
}

Spring对象创建的三种方式:


创建工厂类StudentFactory.java:


package com.wu.pojo;

public class StudentFactory {
public static Student getStudentStatic() {
return new Student();
}
public Student getStudent() {
return new Student();
}
}

在src下创建sprinp的配置文件applicationContext.xml文件:



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">




















创建测试类TestDemo.java:


package com.wu.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.wu.pojo.Student;

public class TestDemo {
@Test
public void test() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Student s1 = ac.getBean("student1",Student.class);
System.out.println("无参构造方法创建对象:"+s1);
Student s2 = ac.getBean("student2",Student.class);
System.out.println("有参构造方法创建对象:"+s2);
Student s3 = ac.getBean("student3",Student.class);
System.out.println("实例工厂创建对象:"+s3);
Student s4 = ac.getBean("student4",Student.class);
System.out.println("静态工厂创建对象:"+s4);

// 获取容器中管理的所有对象名
String[] names = ac.getBeanDefinitionNames();
for(String name : names) {
System.out.println(name);
}

}
}

结果:



属性注入:


applicationContext.xml:



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">





2020




1
2
3
4





1
2
3
4




1
2
3
4













Student.java:


package com.wu.pojo;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Student {
private String name;
private long id;
private Set sets;
private List lists;
private String[] strs;
private Map maps;

public Map getMaps() {
return maps;
}
public void setMaps(Map maps) {
this.maps = maps;
}
public String[] getStrs() {
return strs;
}
public void setStrs(String[] strs) {
this.strs = strs;
}
public List getLists() {
return lists;
}
public void setLists(List lists) {
this.lists = lists;
}
public Set getSets() {
return sets;
}
public void setSets(Set sets) {
this.sets = sets;
}
public Student() {}
public Student(String name,long id) {
this.name = name;
this.id = id;
}
public Student(long id,String name) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Override
public String toString() {
return "Student [name=" + name + ", id=" + id + ", sets=" + sets + ", lists=" + lists + ", strs="
+ Arrays.toString(strs) + ", maps=" + maps + "]";
}
}


TestDemo.java:


package com.wu.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.wu.pojo.Student;

public class TestDemo {
@Test
public void test() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Student s1 = ac.getBean("student1",Student.class);
System.out.println("通过setter赋值:"+s1);

// 获取容器中管理的所有对象名
String[] names = ac.getBeanDefinitionNames();
for(String name : names) {
System.out.println(name);
}

}
}


结果:


通过setter赋值:Student [name=张三, id=2020, sets=[1, 2, 3, 4], lists=[1, 2, 3, 4], strs=[1, 2, 3, 4], maps={key1=value1, key2=value2}]
student1

DI(依赖注入)


新建Book.java:


package com.wu.pojo;

public class Book {
private String name;
private String author;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Book [name=" + name + ", author=" + author + "]";
}
}

修改Student.java:


package com.wu.pojo;

public class Student {
private String name;
private long id;
private Book book;
@Override
public String toString() {
return "Student [name=" + name + ", id=" + id + ", book=" + book + "]";
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}

修改applicationContext.xml:



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">





2020





三体


刘慈欣




结果:


依赖注入:Student [name=张三, id=2020, book=Book [name=三体, author=刘慈欣]]
student1
book

Mybatis整合


在pom.xml文件中新增包:




org.mybatis
mybatis-spring
1.3.2



org.springframework
spring-jdbc
5.2.9.RELEASE



org.springframework
spring-tx
5.2.9.RELEASE



org.springframework
spring-web
5.2.9.RELEASE



org.springframework
spring-test
5.2.9.RELEASE



mysql
mysql-connector-java
8.0.22



javax.servlet
javax.servlet-api
4.0.1


javax.servlet
jstl
1.2


taglibs
standard
1.1.2


在src文件夹下创建Spring全局配置文件applicationContext.xml,内容为:



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">

























创建pojo,Student.java:


package com.wu.pojo;

public class Student {
private String name;
private long id;
private String grade;
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Override
public String toString() {
return "Student [name=" + name + ", id=" + id + ", grade=" + grade + "]";
}
}

创建映射接口StudentMapper.java:


package com.wu.mapper;

import java.util.List;

import com.wu.pojo.Student;

public interface StudentMapper {
List findAll();
}


创建配置Mybatis的映射文件StudentMapper.xml:



PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">




创建业务类接口StudentService.java:


package com.wu.service;

import java.util.List;

import com.wu.pojo.Student;

public interface StudentService {
List show();
}


创建业务接口实现类StudentServiceMapper.java:


package com.wu.service;

import java.util.List;

import com.wu.mapper.StudentMapper;
import com.wu.pojo.Student;

public class StudentServiceImpl implements StudentService{
private StudentMapper studentmapper;


public StudentMapper getStudentmapper() {
return studentmapper;
}


public void setStudentmapper(StudentMapper studentmapper) {
this.studentmapper = studentmapper;
}
@Override
public List show() {
return studentmapper.findAll();
}

}


创建Servlet:


package com.wu.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.wu.service.StudentService;

@WebServlet(name = "servlet" , value = {"/findAllStudent"})
public class Myservlet extends HttpServlet{

private static final long serialVersionUID = -6251824731945077927L;
private StudentService studentservice;
@Override
public void init(ServletConfig config) throws ServletException {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
studentservice = ac.getBean("studentservice",StudentService.class);
}
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
req.setAttribute("studentlist", studentservice.show());
try {
req.getRequestDispatcher("index.jsp").forward(req,res);
}catch(IOException | ServletException e) {
e.printStackTrace();
}
}
}

创建视图层,Index.jsp:


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core"%>




Spring&Mybatis



学生信息表













姓名 学号 班级
${s.name } ${s.id } ${s.grade }

相关推荐

最新更新

猜你喜欢