写配置文件

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>ssm_bookstore</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>bookstore</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:spring_MVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
 </servlet>
  <servlet-mapping>
    <servlet-name>bookstore</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:application_Context.xml</param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

spring_MVC.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" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

<mvc:annotation-driven />
<context:component-scan base-package="com.bs.admin.controller"/>

<!-- 视图构造器   视图对象 -->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
	<property name="prefix" value="/WEB-INF/jsp/" />
	<property name="suffix" value=".jsp" />
</bean>

<!-- 文件上传会用到 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="2048000000"/>
    <property name="maxInMemorySize" value="409600000"/>
    <property name="maxUploadSizePerFile" value="2048000000"/>
    <property name="defaultEncoding" value="UTF-8"/>
    <property name="resolveLazily" value="true"/>
</bean>

<!-- 注册拦截器 -->
<mvc:interceptors>
		<mvc:interceptor>
				<mvc:mapping path="/**"/>
				<bean class="com.bs.admin.interceptor.BookInterceptor"/>
		</mvc:interceptor>
</mvc:interceptors>

application_Context.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" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

<context:component-scan base-package="com.bs.admin">
		<context:exclude-filter type="regex" expression="$[A-Z]\w*Controller^"/>
</context:component-scan>
<!-- 导入spring myBatis整合的配置文件 -->
<import resource="classpath*:spring_mybatis.xml"/>
</beans>

spring_mabatis.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" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	
<!-- 选择并配置数据源,使用c3p0.ComboPooledDataSource会自动加载classpath下的c3p0.properties -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" />

<!-- 生成SessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="dataSource" ref="dataSource" />
	<!-- 扫描实体类 -->
	<property name="typeAliasesPackage" value="com.bs.admin.pojo" />
	<!-- 扫描XxxMapper.xml, mapperLocations:指明Mapper.xml文件路径 -->
	<property name="mapperLocations" value="classpath*:com/bs/admin/mapper/xml/*Mapper.xml" />
	<!-- configuration:配置 -->
	<property name="configuration">
		<bean class="org.apache.ibatis.session.Configuration">
			<!-- mapUnderscoreToCamelCase:将下划线映射到驼峰大小写 -->
			<property name="mapUnderscoreToCamelCase" value="true" />
			<!-- cacheEnabled:**缓存 -->
			<property name="cacheEnabled" value="true"/>
		</bean>
	</property>
</bean>


<!-- 注册接口文件 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<property name="basePackage" value="com.bs.admin.mapper" />
</bean>

<!-- 事务控制 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource" />
</bean>

<!-- 开启注解式事务 -->
<!-- <tx:annotation-driven/> -->
<tx:advice id="txAdvice" transaction-manager="txManager">
	<tx:attributes>
		<!-- read-only="true" :  告诉mybatis只需要创建一个只读的Connection -->
		<tx:method name="add*" propagation="REQUIRED" rollback-for="java.sql.SQLException" />
		<tx:method name="delete*" propagation="REQUIRED" rollback-for="java.sql.SQLException" />
		<tx:method name="modify*" propagation="REQUIRED" rollback-for="java.sql.SQLException" />
		<tx:method name="get*" read-only="true"/>
	</tx:attributes>
</tx:advice>

<!-- 配置aop切口 -->
<aop:config>
	<aop:advisor advice-ref="txAdvice" pointcut="execution(public * com.bs.admin.service..*.*(..))" />
</aop:config>

</beans>

创建表

t_book
spring-ssm整合之多对多查询

t_author
spring-ssm整合之多对多查询

t_book_author 创建多对多的关系需要中间表
spring-ssm整合之多对多查询

pojo实体类

书的实体类

package com.bs.admin.pojo;

    import java.io.Serializable;
    import java.util.HashSet;
    import java.util.Set;

/**
 * 
* <p>Title: Book</p>  
* <p>Description: Book实体类</p>  
* @author Jack 
* <p> @date 2018年11月21日</p>
 */
public class Book implements Serializable {

private static final long serialVersionUID = 597733879789573558L;
private Long primaryId;
private Long bookId;
private String bookName;
private Set<Author> authors = new HashSet<Author>(); // 作者与书,多对多
private String bookCategory;  // 书的种类
private Long bookPurchasePrice;  // 进价
private Long bookSalesPrice;  // 售价
private String bookProfile;  // 简介
private String bookCoverImage;  // 图片路径
private Long publisherId;  // 出版社id
private Long printId;  // 印刷id
private Set<Activity> activity = new HashSet<Activity>();  // 活动与书多对多

public Book(){
	
}
public Book(Long primaryId, Long bookId, String bookName, Set<Author> authors, String bookCategory, Long bookPurchasePrice,Long bookSalesPrice, String bookProfile, String bookCoverImage, Long publisherId, Long printId, Set<Activity> activity) {
	this.primaryId = primaryId;
	this.bookId = bookId;
	this.bookName = bookName;
	this.authors = authors;
	this.bookCategory = bookCategory;
	this.bookPurchasePrice = bookPurchasePrice;
	this.bookSalesPrice = bookSalesPrice;
	this.bookProfile = bookProfile;
	this.bookCoverImage = bookCoverImage;
	this.publisherId = publisherId;
	this.printId = printId;
	this.activity = activity;
}
@Override
public String toString() {
	return "Book [主键:" + primaryId + ", 书号:" + bookId + ", 书名:" + bookName +", 作者:"+ authors + ", 种类:" + bookCategory + ", 进价:" + bookPurchasePrice + ", 售价:" + bookSalesPrice
			+ ", 简介:" + bookProfile + ", 图片路径:" + bookCoverImage + ", 出版者id:" + publisherId + ", 印刷商id:" + printId +", 活动:" + activity + "]";
}
public Long getPrimaryId() {
	return primaryId;
}
public void setPrimaryId(Long primaryId) {
	this.primaryId = primaryId;
}
public Long getBookId() {
	return bookId;
}
public void setBookId(Long bookId) {
	this.bookId = bookId;
}
public String getBookName() {
	return bookName;
}
public void setBookName(String bookName) {
	this.bookName = bookName;
}
public String getBookCategory() {
	return bookCategory;
}
public void setBookCategory(String bookCategory) {
	this.bookCategory = bookCategory;
}
public Long getBookPurchasePrice() {
	return bookPurchasePrice;
}
public void setBookPurchasePrice(Long bookPurchasePrice) {
	this.bookPurchasePrice = bookPurchasePrice;
}
public Long getBookSalesPrice() {
	return bookSalesPrice;
}
public void setBookSalesPrice(Long bookSalesPrice) {
	this.bookSalesPrice = bookSalesPrice;
}
public String getBookProfile() {
	return bookProfile;
}
public void setBookProfile(String bookProfile) {
	this.bookProfile = bookProfile;
}
public String getBookCoverImage() {
	return bookCoverImage;
}
public void setBookCoverImage(String bookCoverImage) {
	this.bookCoverImage = bookCoverImage;
}
public Long getPublisherId() {
	return publisherId;
}
public void setPublisherId(Long publisherId) {
	this.publisherId = publisherId;
}
public Long getPrintId() {
	return printId;
}
public void setPrintId(Long printId) {
	this.printId = printId;
}
public Set<Activity> getActivity() {
	return activity;
}
public void setActivity(Set<Activity> activity) {
	this.activity = activity;
}
public Set<Author> getAuthors() {
	return authors;
}
public void setAuthors(Set<Author> authors) {
	this.authors = authors;
}
}

作者的实体类

package com.bs.admin.pojo;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
 * 
* <p>Title: Author</p>  
* <p>Description: 作者的实体类</p>  
* @author  jack
* <p> @date 2018年11月21日</p>
 */
public class Author implements Serializable {

private static final long serialVersionUID = -2631951426846305889L;
private Integer authorId;
private String authorName;
private Set<Book> books = new HashSet<Book>();

public Author(){
	
}
public Author(Integer authorId, String authorName, Set<Book> books) {
	this.authorId = authorId;
	this.authorName = authorName;
	this.books = books;
}
@Override
public String toString() {
	return "Author [authorId=" + authorId + ", authorName=" + authorName + ", book:" + books +"]";
}
public Integer getAuthorId() {
	return authorId;
}
public void setAuthorId(Integer authorId) {
	this.authorId = authorId;
}
public String getAuthorName() {
	return authorName;
}
public void setAuthorName(String authorName) {
	this.authorName = authorName;
}
public Set<Book> getBooks() {
	return books;
}
public void setBooks(Set<Book> books) {
	this.books = books;
}
}

Mapper层

BookMapper.xml

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

<mapper namespace="com.bs.admin.mapper.BookMapper">
<!-- 通过bookId查询书的信息 -->
  <select id="getBookByBookId" resultMap="BookResultMap">
  		select <include refid="bookColumns"/>,<include refid="authorColumns"/>
		from t_book b,t_author a,t_book_author ba 
		where ba.book_id = b.book_id 
		and ba.author_id = a.author_id 
		and b.book_id = #{bookId}
  </select>

<resultMap type="book" id="BookResultMap" autoMapping="true">
		<id property="primaryId" column="primary_id"/>
		<!-- 多对多查询 -->
		<collection property="authors" ofType="author" autoMapping="true"/>
</resultMap>

<sql id="bookColumns">
		primary_id, b.book_id,b.book_name,b.book_category,b.book_purchasePrice,b.book_salesPrice,
		b.book_profile,b.book_coverImage,b.publisher_id,b.print_id     
</sql>

<sql id="authorColumns">
		a.author_id, a.author_name
</sql>

</mapper>

BookMapper接口

package com.bs.admin.mapper;

import com.bs.admin.pojo.Book;

public interface BookMapper {

	Book getBookByBookId(Long bookId);
	
	Integer createBook(Book book); 
}

dao层

接口

package com.bs.admin.dao;

import org.springframework.stereotype.Repository;

import com.bs.admin.pojo.Book;

@Repository
public interface BookDao {

	Book retrieveBookByBookId(Long bookId); 
	
}

实现类

package com.bs.admin.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.bs.admin.dao.BookDao;
import com.bs.admin.mapper.BookMapper;
import com.bs.admin.pojo.Book;

@Repository
public class BookDaoImpl implements BookDao {

	@Autowired
	private BookMapper bm;
	
	@Override
	public Book retrieveBookByBookId(Long bookId) {
		Book book = bm.getBookByBookId(bookId);
		return book;
	}

}

service层

接口

package com.bs.admin.service;

import org.springframework.stereotype.Service;

import com.bs.admin.pojo.Book;

@Service
public interface  BookService {

	Book getBookByBookId(Long bookId);
}

实现类

package com.bs.admin.service.impl;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bs.admin.dao.BookDao;
import com.bs.admin.dao.impl.BookDaoImpl;
import com.bs.admin.pojo.Book;
import com.bs.admin.service.BookService;

@Service
public class BookServiceImpl implements BookService{

	@Autowired
	private BookDao bd; 
	
	@Override
	public Book getBookByBookId(Long bookId) {
		Book book = bd.retrieveBookByBookId(bookId);
		return book;
	}
}

controller层

BookstoreController

package com.bs.admin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.bs.admin.pojo.Author;
import com.bs.admin.pojo.Book;
import com.bs.admin.service.AuthorService;
import com.bs.admin.service.BookService;

@RestController
@RequestMapping("bookstore")
public class BookstoreController {

	@Autowired
	private BookService bs;
	
	@Autowired
	private AuthorService as;
	
	@GetMapping("{bookId}/getBook")
	public @ResponseBody Book getBook(@PathVariable("bookId") Long bookId) {
		Book book = bs.getBookByBookId(bookId);
		System.out.println("book对象:   " + book);
		return book;
	}
}

浏览器
spring-ssm整合之多对多查询

控制台
spring-ssm整合之多对多查询

相关文章: