MyBatis多對多增刪改查(不建關聯實體)
寫在前面
本文的出發點在於:本人師傅比較NB,在專案中資料庫操作用的MyBatis,需要涉及到多對多的操作,問師傅多對多是否需要建立關聯實體,師傅說了句“低階的可以建,高階的不需要”,於是乎,為了挑戰難度,在專案中最終以不建立關聯實體解決了多對多的操作問題。於是空閒的時間自己用
spring-mvc 4
、MyBatis
、MySQL
、Maven
從零搭建起一個 簡單的demo,一是為了從頭到尾鞏固理解,二是為了留著以後忘記了可以查閱,三是也希望能幫助到一些像我這樣菜鳥級別的人。此片文章可能非常長,我寫的步驟也算很細了,NB的人肯定認為很累贅,不過重在分享!
環境工具
- 作業系統:Windows 7
- 開發工具:Intellij IDEA 14
- 資料庫連線:Navicat 11 Premium
- 介面測試:Postman
demo要完成的功能
- 對學生student的操作:學生學號必須唯一
- 新增學生資訊;
- 修改學生資訊;
- 按學號查詢學生資訊;
- 查詢所有學生資訊(不載入所選教師資訊);
- 查詢所有學生資訊(同時載入所選教師資訊);
- 對教師teacher的操作:教師工號必須唯一
- 新增教師資訊;
- 修改教師資訊;
- 按工號查詢教師資訊;
- 查詢所有教師資訊(不載入所教學生資訊);
- 查詢所有教師資訊(同時載入所教學生資訊);
- 對關聯表student_teacher的操作:學生選授課教師
- 學生選擇多個授課教師,關聯資訊插入關聯表中
demo依賴jar包說明
- junit-3.8.1.jar:用於單元測試;
- spring-jdbc-4.1.6.RELEASE.jar:事務管理;
- spring-webmvc-4.1.6.RELEASE.jar:spring-mvc 4必須;
- spring-aspects-4.1.6.RELEASE.jar:面向切面程式設計;
- mybatis-3.2.8.jar:MyBatis必須;
- mybatis-spring-1.2.2.jar:MyBatis整合spring所需;
- commons-dbcp-1.4.jar:資料庫連線池;
- slf4j-log4j12-1.7.7.jar:列印日誌;
- servlet-api-2.5.jar:servlet容器所需
- fastjson-1.1.36.jar:json資料轉換工具;
- jackson-databind-2.3.3.jar:序列化類為json格式資料所需;
- mysql-connector-java-5.1.32.jar:java連線MySQL資料庫驅動包
開發步驟
第一步:新建資料庫
1.ER圖:
2.student表:
3.teacher表:
4.關聯表student_teacher:
5.關聯表增加外來鍵與student和teacher關聯:
第二步:新建Maven工程
1.新建maven webapp工程:
2.填寫maven座標:
3.新建完成的工程:
4.配置maven:
(1).maven home directory:maven解壓後所在的路徑;
(2).usere settings file:maven conf目錄下的setting檔案,具體配置略;
(3).local repository:本地maven倉庫所在的目錄,用於存放jar
第三步:搭建專案結構
新建entity包、dao包、service包、impl包、controller包,結構按需自定義
第四步:配置開發環境引數
修改專案編碼:三處均為 “UTF-8”
第五步:增加所有jar依賴到pom.xml
新增專案所需的所有jar的maven座標:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.36</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
<scope>runtime</scope>
</dependency>
</dependencies>
第六步:配置application.development.properties和applicationContext.xml
- application.development.properties:資料庫連線引數以及資料庫連線池引數;
#mysql database setting
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1/mybatis-demo?useUnicode=true&characterEncoding=utf-8
jdbc.username=user
jdbc.password=123456
#connection pool settings
jdbc.pool.minIdle=3
jdbc.pool.maxIdle=5
jdbc.pool.maxActive=15
jdbc.pool.maxWait=120000
2 . applicationContext.xml:spring容器核心配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
default-lazy-init="true">
<description>Spring公共配置</description>
<!-- 該 BeanPostProcessor 將自動對標註 @Autowired 的 Bean 進行注入 -->
<context:annotation-config/>
<!-- 使用annotation 自動註冊bean, 並保證@Required、@Autowired的屬性被注入 -->
<context:component-scan base-package="com.xiaolong.demo"/>
<!-- MyBatis配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 自動掃描entity目錄, 省掉Configuration.xml裡的手工配置 -->
<property name="typeAliasesPackage" value="com.xiaolong.demo"/>
<!-- 顯式指定Mapper檔案位置 -->
<property name="mapperLocations" value="classpath:/mybatis/*Mapper.xml"/>
</bean>
<!-- 掃描basePackage下所有以@MyBatisRepository標識的 介面 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xiaolong.demo.dao"/>
<property name="annotationClass"
value="com.xiaolong.demo.dao.MyBatisRepository"/>
</bean>
<!-- 使用annotation定義事務 -->
<tx:annotation-driven proxy-target-class="true"/>
<bean id="transactionManager" name="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<!--<tx:attributes>-->
<!--<tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/>-->
<!--</tx:attributes>-->
</tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut id="transactionPointcut"
expression="execution(* com.xiaolong.demo.service.impl.*.*(..))"/>
<aop:advisor pointcut-ref="transactionPointcut"
advice-ref="transactionAdvice"/>
</aop:config>
<!-- local development環境 -->
<beans profile="development">
<context:property-placeholder
ignore-resource-not-found="true"
location="classpath*:/application.development.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive">
<value>${jdbc.pool.maxActive}</value>
</property>
<property name="maxIdle">
<value>${jdbc.pool.maxIdle}</value>
</property>
<property name="minIdle">
<value>${jdbc.pool.minIdle}</value>
</property>
<property name="maxWait">
<value>${jdbc.pool.maxWait}</value>
</property>
</bean>
<bean id="studentService" class="com.xiaolong.demo.service.impl.StudentServiceImpl" />
<bean id="teacherService" class="com.xiaolong.demo.service.impl.TeacherServiceImpl" />
</beans>
</beans>
第七步:配置spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 自動掃描且只掃描@Controller -->
<context:component-scan base-package="com.xiaolong.demo" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!--<context:property-placeholder location="/WEB-INF/*.properties"/>-->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<!-- 將StringHttpMessageConverter的預設編碼設為UTF-8 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
</beans>
第七步:新建實體Student和Teacher
Student實體:
public class Student {
private Integer id; //id
private String name; //姓名
private String number; //學號,保證唯一
private String gender; //性別
private Integer age; //年齡
private List<Teacher> teachers; //授課老師集合
..........省略get、set............
}
Teacher實體:
public class Teacher {
private Integer id; //id
private String name; //姓名
private String number; //工號,保證唯一
private String gender; //性別
private Integer age; //年齡
private List<Student> students; //所教學生集合
............省略get、set方法.....................
}
第八步:新建StudentMyBatisDao介面
在此之前需要先寫個註解類MyBatisRepository
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface MyBatisRepository {
}
然後新建StudentMyBatisDao介面
@MyBatisRepository
public interface StudentMyBatisDao {
int addStudent(Student student);
int updateStudent(Student student);
Student findStudentByNumber(String number);
Student findStudentById(Long id);
List<Student> findAll();
List<Student> findAllWithTeachers();
int selectTeachers(Long studentId, List<Long> teacherIds);
}
第九步:編寫MyBatis的StudentMapper.xml
說明:這裡我使用了查詢結果的繼承關係,當不需要查詢列表是resultMap就寫studentMap,當需要將教師資訊同時載入時返回的resultMap就是studentTeacherMap。另外,多對多在插入關聯表的時候我使用的是批量插入,用了foreach,param2代表第二個集合引數
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace必須指向Dao介面 -->
<mapper namespace="com.xiaolong.demo.dao.StudentMyBatisDao">
<!--查詢結果不包含app列表-->
<resultMap id="studentMap" type="com.xiaolong.demo.entity.Student">
<id column="id" property="id" jdbcType="BIGINT"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="number" property="number" jdbcType="VARCHAR"/>
<result column="gender" property="gender" jdbcType="VARCHAR"/>
<result column="age" property="age" jdbcType="INTEGER"/>
</resultMap>
<!--查詢的時候將教師列表一起查詢出來-->
<resultMap id="studentTeacherMap" type="com.xiaolong.demo.entity.Student"
extends="studentMap">
<collection property="teachers" ofType="com.xiaolong.demo.entity.Teacher">
<id property="id" column="t_id" jdbcType="BIGINT"/>
<result property="name" column="t_name" jdbcType="VARCHAR"/>
<result property="number" column="t_number" jdbcType="VARCHAR"/>
<result property="gender" column="t_gender" jdbcType="VARCHAR"/>
<result property="age" column="t_age" jdbcType="INTEGER"/>
</collection>
</resultMap>
<sql id="columns">
id,name,number,gender,age
</sql>
<select id="findStudentByNumber" resultMap="studentMap" parameterType="java.lang.String">
SELECT
<include refid="columns"/>
FROM student
WHERE number=#{0}
</select>
<select id="findStudentById" resultMap="studentTeacherMap" parameterType="java.lang.Long">
SELECT
s.id,s.name,s.number,s.gender,s.age,
t.id as t_id,t.name as t_name,t.number as t_number,t.gender as t_gender,t.age as t_age
FROM
student s LEFT JOIN student_teacher st
ON s.id=st.s_id
LEFT JOIN teacher t
ON t.id=st.t_id
WHERE s.id=#{0}
</select>
<select id="findAll" resultMap="studentMap">
SELECT * FROM student
</select>
<select id="findAllWithTeachers" resultMap="studentTeacherMap">
SELECT
s.id,s.name,s.number,s.gender,s.age,
t.id as t_id,t.name as t_name,t.number as t_number,t.gender as t_gender,t.age as t_age
FROM
student s LEFT JOIN student_teacher st
ON s.id=st.s_id
LEFT JOIN teacher t
ON t.id=st.t_id
</select>
<insert id="addStudent" parameterType="com.xiaolong.demo.entity.Student">
insert into student
(
name,number,gender,age
)
values
(
#{name,jdbcType=VARCHAR},#{number,jdbcType=VARCHAR},#{gender,jdbcType=VARCHAR},#{age,jdbcType=INTEGER}
)
</insert>
<update id="updateStudent" parameterType="com.xiaolong.demo.entity.Student">
update student
<set>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
<if test="number != null">
number = #{number,jdbcType=VARCHAR},
</if>
<if test="gender != null">
gender = #{gender,jdbcType=VARCHAR},
</if>
<if test="age != null">
age = #{age,jdbcType=INTEGER},
</if>
</set>
where id=#{id,jdbcType=BIGINT}
</update>
<!--/*這種方式使用批量插入*/-->
<insert id="selectTeachers">
INSERT INTO student_teacher
(s_id,t_id)
VALUES
<foreach collection="param2" item="id" separator=",">
(#{0}, #{id})
</foreach>
</insert>
</mapper>
第十步:編寫StudentService介面
功能包括:對學生的增刪改查以及學生選擇教師
public interface StudentService {
public void addStudent(Student student) throws Exception;
public void updateStudent(Student student) throws Exception;
public Student findStudentByNumber(String number);
public List<Student> findAll();
public List<Student> findAllWithTeachers();
public void selectTeachers(Long studentId,List<Long> teacherIds)throws Exception;
}
第十一步:編寫StudentServiceImpl實現類
在新增和修改是我手動判斷了學號是否重複
package com.xiaolong.demo.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.xiaolong.demo.dao.StudentMyBatisDao;
import com.xiaolong.demo.entity.Student;
import com.xiaolong.demo.entity.Teacher;
import com.xiaolong.demo.service.StudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* Created by xiaolong.zhu on 2015/6/4.
*/
@SuppressWarnings("ALL")
@Transactional
@Component
public class StudentServiceImpl implements StudentService {
private static final Logger logger = LoggerFactory.getLogger(StudentServiceImpl.class);
@Autowired
private StudentMyBatisDao studentMyBatisDao;
@Override
public void addStudent(Student student) throws Exception {
logger.info("新增學生資訊{}", JSONObject.toJSONString(student));
Student another = studentMyBatisDao.findStudentByNumber(student.getNumber());
if (another != null && another.getId() != student.getId())
throw new Exception("引數異常,number重複");
int result = studentMyBatisDao.addStudent(student);
if (result != 1)
throw new Exception("新增學生資訊失敗");
}
@Override
public void updateStudent(Student student) throws Exception {
logger.info("修改學生資訊{}", JSONObject.toJSONString(student));
if (student.getId() == null)
throw new Exception("引數異常,id為null");
Student another = studentMyBatisDao.findStudentByNumber(student.getNumber());
if (another != null && another.getId() != student.getId())
throw new Exception("引數異常,number重複");
int result = studentMyBatisDao.updateStudent(student);
if (result != 1)
throw new Exception("修改學生資訊失敗");
}
@Override
public Student findStudentByNumber(String number) {
logger.info("通過學號{}查詢學生資訊", number);
Student student = studentMyBatisDao.findStudentByNumber(number);
return student;
}
@Override
public List<Student> findAll() {
logger.info("查詢所有學生資訊");
return studentMyBatisDao.findAll();
}
@Override
public List<Student> findAllWithTeachers() {
logger.info("查詢所有學生資訊及授課老師");
return studentMyBatisDao.findAllWithTeachers();
}
@Override
public void selectTeachers(Long studentId, List<Long> teacherIds) throws Exception {
logger.info("學生{}選擇授課老師{}", studentId, teacherIds.toArray());
Student student = studentMyBatisDao.findStudentById(studentId);
for (Teacher teacher : student.getTeachers()) {
if (teacher != null) {
if (teacherIds.contains(teacher.getId()))
throw new Exception("引數異常,該學生" + studentId + "已經選擇了此教師" + teacher.getId() + ",不允許重複操作");
}
}
int result = studentMyBatisDao.selectTeachers(studentId, teacherIds);
if (result != teacherIds.size())
throw new Exception("引數異常,教師id錯誤");
}
}
在完成StudentServiceImpl之後,為了在Controller中能用@Autowired
自動注入,需要將StudentServiceImpl在spring容器中註冊,見applicationContext.xml
第十二步:編寫StudentController類
說明:由於用的spring mvc 4,所以@ResponseBody
就不用在寫了,它已經寫在了註解類@RestController
中了。另外,ip是獲取客戶端真是ip,若使用了代理,就從header中取得,未使用代理,就使用request.getRemoteHost()
得到。
package com.xiaolong.demo.controller;
import com.alibaba.fastjson.JSONObject;
import com.xiaolong.demo.entity.Student;
import com.xiaolong.demo.service.StudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
* Created by xiaolong.zhu on 2015/6/4.
*/
@SuppressWarnings("SpringJavaAutowiringInspection")
@RestController
@RequestMapping(value = "/demo/", consumes = {MediaType.ALL_VALUE})
public class StudentController {
private static final Logger logger = LoggerFactory.getLogger(StudentController.class);
@Autowired
private StudentService studentService;
@RequestMapping(value = "student", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity addStudent(@RequestBody Student student, HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
if (ip == null)
ip = request.getRemoteHost();
logger.info("ip {} 正新增新的學生資訊{}", ip, JSONObject.toJSONString(student));
try {
studentService.addStudent(student);
return new ResponseEntity(null, HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
logger.info(e.getMessage());
JSONObject error = new JSONObject();
error.put("error", e.getMessage());
return new ResponseEntity(error, HttpStatus.BAD_REQUEST);
}
}
@RequestMapping(value = "student", method = RequestMethod.PUT, consumes = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity updateStudent(@RequestBody Student student, HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
if (ip == null)
ip = request.getRemoteHost();
logger.info("ip {} 正修改學生資訊{}", ip, JSONObject.toJSONString(student));
try {
studentService.updateStudent(student);
return new ResponseEntity(null, HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
logger.info(e.getMessage());
JSONObject error = new JSONObject();
error.put("error", e.getMessage());
return new ResponseEntity(error, HttpStatus.BAD_REQUEST);
}
}
@RequestMapping(value = "student/{number}", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity findByNumber(@PathVariable("number") String number,
HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
if (ip == null)
ip = request.getRemoteHost();
logger.info("ip {} 正通過學號{}獲取學生資訊", ip, number);
Student student = studentService.findStudentByNumber(number);
return new ResponseEntity(student, HttpStatus.OK);
}
@RequestMapping(value = "students", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity findAll(HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
if (ip == null)
ip = request.getRemoteHost();
logger.info("ip {} 獲取所有學生資訊", ip);
List<Student> students = studentService.findAll();
return new ResponseEntity(students, HttpStatus.OK);
}
@RequestMapping(value = "students/teachers", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity findAllWithTeachers(HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
if (ip == null)
ip = request.getRemoteHost();
logger.info("ip {} 獲取所有學生資訊以及其授課老師", ip);
List<Student> students = studentService.findAllWithTeachers();
return new ResponseEntity(students, HttpStatus.OK);
}
@RequestMapping(value = "student/{id}/teachers", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity selectTeachers(@PathVariable("id") Long studentId,
@RequestBody JSONObject ids,
HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
if (ip == null)
ip = request.getRemoteHost();
logger.info("ip {} 學生{}選擇授課老師{}",ip,studentId,ids.toJSONString());
try {
studentService.selectTeachers(studentId, (List<Long>) ids.get("teacherIds"));
return new ResponseEntity(null, HttpStatus.OK);
}catch (Exception e) {
e.printStackTrace();
logger.info(e.getMessage());
JSONObject error = new JSONObject();
error.put("error", e.getMessage());
<