整合SSM框架应用

普通方式

新建spring模块时引入如下内容:

整合SSM框架应用

启用devtools插件(热部署插件)

idea需要做如下配置

settings-build-compiler->勾选build project autoxxx选项

shift+alt+ctrl+/ ->registry->勾选compiler.automake.allow.when.app.running

在springboot插件里做如下配置

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--必须配置devtools-->
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>

启用lombok(通过注解生成get/set等方法)

settings-plugins-搜索lombok-安装lombok插件

结构:

整合SSM框架应用

使用lombok插件的实体类

package com.example.bootssm1.domain;

import lombok.*;

import java.math.BigDecimal;
import java.util.Date;
//使用了lombok插件
@Data //自动生成get/set/toString等方法
@AllArgsConstructor//生成包含所有变量的构造方法
@NoArgsConstructor//生成无参的构造方法
public class BookInfo {
private Integer bookId;
private String bookName;
private String bookAuthor;
private BigDecimal bookPrice;
private Date bookDate;
}

Mapper类

package com.example.bootssm1.mapper;

import com.example.bootssm1.domain.BookInfo;
import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper // 把该接口生成的代理类的实例交给spring容器控制
public interface BookMapper {
BookInfo getBookById(Integer bookId);
int saveBook(BookInfo bookInfo);
int batchBook(List<BookInfo> books);
}

Mapper配置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.example.bootssm1.mapper.BookMapper">
<select id="getBookById" resultType="bookInfo">
select book_id, book_name, book_author, book_price, book_date
from t_book where book_id = #{id}
</select> <insert id="saveBook" parameterType="bookInfo">
insert into t_book(book_name, book_author, book_price, book_date)
values(#{bookName}, #{bookAuthor}, #{bookPrice}, #{bookDate})
</insert> <insert id="batchBook" parameterType="list">
insert into t_book(book_name, book_author, book_price, book_date)
values
<foreach collection="books" item="val" open="(" close=")" separator=",">
#{val.bookName}, #{val.bookAuthor}, #{val.bookPrice},#{val.bookDate}
</foreach>
</insert>
</mapper>

application.yml

spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///testdb?useSSL=true
username: root
password: 123
mybatis:
configuration:
map-underscore-to-camel-case: true # book_id => bookId,book_name=> bookName 映射字段名到实体类的属性
mapper-locations: classpath:mapper/*Mapper.xml #指定mapper.xml的路径
type-aliases-package: com.example.bootssm1.domain # 定义别名名称 默认为实体类类名首字母小写

测试类:

package com.example.bootssm1;

import com.example.bootssm1.domain.BookInfo;
import com.example.bootssm1.mapper.BookMapper;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; @RunWith(SpringRunner.class)
@SpringBootTest
public class BootSsm1ApplicationTests { @Resource
private BookMapper bookMapper; @Test
public void selectOne() {
BookInfo book = bookMapper.getBookById(2);
System.out.println(book);
} @Test
public void save() {
BookInfo book = bookMapper.getBookById(2);
book.setBookName("新书");
int row = bookMapper.saveBook(book);
Assert.assertEquals(1, row);
} }

整合tk.mybatis

结构:

整合SSM框架应用

pom引入依赖

        <dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.4</version>
</dependency>

BaseMapper

package com.example.bootssm2.common;

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper; /**
* 项目中所有Mapper类的父类
* @param <T>
*/
public interface BaseMapper<T> extends Mapper<T>, MySqlMapper<T> {
}

实体类:

package com.example.bootssm2.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor; import javax.persistence.Id;
import javax.persistence.Table;
import java.math.BigDecimal;
import java.util.Date; @Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "t_book")//设定表名
public class BookInfo {
@Id // 设定为主键
private Integer bookId;
private String bookName;
private String bookAuthor;
private BigDecimal bookPrice;
private Date bookDate;
}

具体Mapper(只要继承BaseMapper即可)

package com.example.bootssm2.mapper;

import com.example.bootssm2.common.BaseMapper;
import com.example.bootssm2.domain.BookInfo; public interface BookInfoMapper extends BaseMapper<BookInfo> { }

application.yml

spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///testdb?useSSL=true
username: root
password: 123
mapper:
identity: MYSQL
not-empty: true
mappers:
- tk.mybatis.mapper.common.Mapper
- tk.mybatis.mapper.common.MySqlMapper
#启用日志打印SQL语句
logging:
level:
com.example.bootssm2.mapper: debug

测试类:

package com.example.bootssm2;

import com.example.bootssm2.domain.BookInfo;
import com.example.bootssm2.mapper.BookInfoMapper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import tk.mybatis.spring.annotation.MapperScan; import java.util.Arrays; @SpringBootApplication
@MapperScan("com.example.bootssm2.mapper")
public class BootSsm2Application { public static void main(String[] args) {
ConfigurableApplicationContext
context = SpringApplication.run(BootSsm2Application.class, args);
BookInfoMapper mapper = context.getBean(BookInfoMapper.class);
BookInfo book1 = mapper.selectByPrimaryKey(2);
BookInfo book2 = mapper.selectByPrimaryKey(4);
BookInfo book3 = mapper.selectByPrimaryKey(5); mapper.insertList(Arrays.asList(book1, book2, book3));
}
}
上一篇:使用JS进行pc端、手机端判断


下一篇:jquery完善的处理机制