ssm的整合,整合spring,springmvc,通用mapper简化sql编写,用lombok简化实体类编写。
ssm使用通用mapper时,数据库字段命名如book_id,实体类的命名要为驼峰命名:bookId,否则容易报错。(而数据表的命名可以为tb_books而实体类的名可以为Books,数据库表名和实体类名不一样也可以的,但字段不行)
一般的来说:

实体类是采用驼峰命名的。
package com.kuang.controller;
import com.kuang.pojo.Books;
import com.kuang.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
/**
* @author QLBF
* @version 1.0
* @date 2021/4/14 14:59
*/
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
private BookService bookService;
@RequestMapping("/test")
@ResponseBody
public String ptbjson(){
return "hello 整合!";
}
//输出从mysql查询的集合再转为为json到浏览器
@RequestMapping("/pass")
@ResponseBody
public List<Books> ptbjson1(){
List<Books> books = bookService.queryAllBook();
return books;
}
}
这里是extends实体类Books表(而不是数据库表哦),它就有了增删改查方法,这就是通用mapper的魅力,如果需要自定义自己的sql语句的话,可以在下面加的,后面出章节聊
package com.kuang.mapper;
import com.kuang.pojo.Books;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
/**
* @author QLBF
* @version 1.0
* @date 2021/4/14 15:53
*/
//下面那个Books是你的实体类,ssm整合通用mapper下面@Repository要加,springboot可以不用
@Repository
public interface BooksMapper extends Mapper<Books> {
}
package com.kuang.pojo;
import lombok.Data;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* @author QLBF
* @version 1.0
* @date 2021/4/14 14:46
*/
@Data
@Table(name = "books")
public class Books {
@Id
private int bookId;
private String bookName;
private int bookCounts;
private String detail;
}
package com.kuang.service;
import com.kuang.pojo.Books;
import java.util.List;
/**
* @author QLBF
* @version 1.0
* @date 2021/4/14 14:48
*/
public interface BookService {
List<Books> queryAllBook();
}
package com.kuang.service.impl;
import com.kuang.mapper.BooksMapper;
import com.kuang.pojo.Books;
import com.kuang.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author QLBF
* @version 1.0
* @date 2021/4/14 14:49
*/
@Service(value = "bookService")
public class BookServiceImpl implements BookService {
@Autowired
private BooksMapper booksMapper;
public List<Books> queryAllBook() {
List<Books> books = booksMapper.selectAll();
return books;
}
}
配置文件:
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="spring-dao.xml"/>
<import resource="spring-service.xml"/>
<import resource="spring-mvc.xml"/>
</beans>
jdbc.properties:
(这里记得改为你的数据库名)
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mysqltest
jdbc.username=root
jdbc.password=root
spring-dao.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<!-- 1.关联数据库文件 -->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--2.配置数据源信息-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!--proproperty name是druid里面的set方法,固定死的,不能改-->
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--3.配置spring整合mybatis框架的SQLSessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--声明式事务控制-->
<!--平台事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--事物注解驱动-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 4.通用 Mapper,不用写sql了-->
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.kuang.mapper" />
</bean>
</beans>
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 配置SpringMVC -->
<!-- 1.开启SpringMVC注解驱动 -->
<mvc:annotation-driven />
<!-- 2.静态资源默认servlet配置-->
<mvc:default-servlet-handler/>
<!-- 3.配置jsp 显示ViewResolver视图解析器(本演示的controller没有用到jsp) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 4.扫描web相关的bean -->
<context:component-scan base-package="com.kuang.controller" />
</beans>
spring-service.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置扫描器,扫描Service-->
<context:component-scan base-package="com.kuang.service"/>
</beans>
WEB-INF/web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<!--spring 监听器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--springmvc的前端控制器-->
<servlet>
<servlet-name>DispatcherServlet</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>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--乱码过滤器-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
最后附上数据库:

CREATE TABLE `books` (
`book_id` int(10) NOT NULL AUTO_INCREMENT COMMENT '书id',
`book_name` varchar(100) NOT NULL COMMENT '书名',
`book_counts` int(11) NOT NULL COMMENT '数量',
`detail` varchar(200) NOT NULL COMMENT '描述',
PRIMARY KEY (`book_id`) USING BTREE,
KEY `bookID` (`book_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
INSERT INTO `books` VALUES
(1,'Java',1,'从入门到放弃'),
(2,'MySQL',10,'从删库到跑路'),
(3,'Linux',5,'从进门到进牢');
测试结果:

最后附上项目地址:ssm的整合,整合spring,springmvc,通用mapper简化sql编写,用lombok简化实体类编写
参考:
1.狂神整合ssm
2.通用mapper
最后
以上就是畅快书包最近收集整理的关于SSM简单整合通用mapper案例、驼峰命名法的全部内容,更多相关SSM简单整合通用mapper案例、驼峰命名法内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复