spring boot整合mybatis框架及增删改查(jsp视图)

时间:2024-03-24 14:34:56

工具:idea、SQLyog

版本:springboot1.5.9版本、mysql5.1.62

第一步:新建项目

     spring boot整合mybatis框架及增删改查(jsp视图)

     spring boot整合mybatis框架及增删改查(jsp视图)

第二步:整合依赖(pom.xml)

     <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <!--启动热部署的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>1.3.0.RELEASE</version>
<optional>true</optional>
</dependency>
<!-- 支持视图解析器-->
<!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- 整合mybatis
jdbc mysql的驱动 mybatis和springboot整合依赖 pagehelper依赖
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!-- 后端分页依赖-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.4</version>
</dependency>
<!-- json包 -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<!--文件上传 -->
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency> </dependencies>

第三步:配置application.properties文件

server.port=8086
#编码格式
web.encoding=UTF-8
#热部署自动
spring.devtools.restart.enabled=true
#静态资源
spring.mvc.static-path-pattern=/static/**
#视图解析器
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
#数据源
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/wutongvip?characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
#配置mybatis
#mybatis映射文件路径 路径一定要对应好 如放在static下,classpath:static/mapping/*.xml
mybatis.mapper-locations=classpath:mapping/*.xml
#设置pojo别名
mybatis.type-aliases-package=com.buba.nusmanager.pojo
#驼峰映射开关
mybatis.configuration.map-underscore-to-camel-case=true #pagerHelper分页配置
#数据库方言
#pagehelper.dialect=mysql
#分页合理的 pagerNum<1时 查询结果为为pageNum=1
pagehelper.reasonable=true
#支持从方法的参数中获取页码信息
pagehelper.support-methods-arguments=true
#默认值0
pagehelper.page-size-zero=true
#请求是所带的参数
pagehelper.params==count=countsql #打印mybatis的sql语句
logging.level.com.example.wutongdemo.maper=debug
logging.file=springboot.log #解决图片上传问题
picurl=D:/imgs/ spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/, classpath:/static/, classpath:/public/,file:D:/imgs/

CommodityMaper.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.wutongdemo.maper.CommodityMaper"> <select id="findAllCommodity" resultType="com.example.wutongdemo.pojo.Commodity">
SELECT * FROM `user`
</select> </mapper>

第四步:项目整体架构图

      spring boot整合mybatis框架及增删改查(jsp视图)

第五步:测试运行

    spring boot整合mybatis框架及增删改查(jsp视图)

注意事项:

  1. mapper层加入@Repository注解,service实现类加入@Service注解;
  2. maper.xml文件要和application.properties文件路径对应,maper.xml的namespace属性等于maper层接口绝对路径;
  3. spring boot项目启动类上加入@MapperScan("maper层全路径")。

以上总结希望可以帮到大家,有什么问题及时反馈。