2019独角兽企业重金招聘Python工程师标准>>>

摘要: Spring3.2.0-mybatis3.2.0 基于全注解搭建的后台框架-基础版

没有什么不可能  之前一直用的是自己搭建的spring 和mybatis的框架 但是里面有很多不足

1:mybatis下面的sql使用的mapper xml格式的时候 过于烦了 都要在xml下面修改 然后从启服务。

2:在使用注解的时候 每次都要配置mapperInterface。 在applicationContext这个配置文件里面也好生麻烦。

下面就简单的来解决一下这个问题。

spring-mybatis写业务的时候 全注解的方式 使用,

首先 准备工作 开启本地的mysql数据库 或者是链接好服务器的mysql数据库 把里面的链接配置都拿出来

新建一个web工程

在项目的lib下面 把整理好的spring mybatis mysql的jar全部拷贝到下面

新建一个pojo


import java.io.Serializable;public class User implements Serializable{/*** */private static final long serialVersionUID = 1L;private int userid;private String username;private String password;private String createtime;private String updatetime;private String telephone;private int status;private String nickname;private int integral;public int getUserid() {return userid;}public void setUserid(int userid) {this.userid = userid;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getCreatetime() {return createtime;}public void setCreatetime(String createtime) {this.createtime = createtime;}public String getUpdatetime() {return updatetime;}public void setUpdatetime(String updatetime) {this.updatetime = updatetime;}public String getTelephone() {return telephone;}public void setTelephone(String telephone) {this.telephone = telephone;}public int getStatus() {return status;}public void setStatus(int status) {this.status = status;}public String getNickname() {return nickname;}public void setNickname(String nickname) {this.nickname = nickname;}public int getIntegral() {return integral;}public void setIntegral(int integral) {this.integral = integral;}
}

新建一个UserProvider类 写一个查询全部的方法 这个类主要就是返回sql 留给mybatis去执行

public class UserProvider {/*** 在这个类下面编写user岁要用的sql* * 更新时间 2015年5月18日13:46:00* * 新浪微博 @Steven_LuYang*/// 使用和mapperxml格式是一样的格式private static final String COLUMNS = " USERID,USERNAME,PASSWORD,CREATETIME,UPDATETIME,TELEPHONE,STATUS,NICKNAME,INTEGRAL ";private static final String TABLENAME = "user";// 查询所有的sql 所有的方法返回一个sql就可以 这边的方法名称和mapper下面的属性的method是一致的// 2015年5月18日13:47:05public String all() {return Variable.DB_SELECT + COLUMNS + Variable.DB_FROM + TABLENAME;}public String list(Map<String, Object> map){StringBuffer sqlBuffer = new StringBuffer();sqlBuffer.append(Variable.DB_SELECT + COLUMNS + Variable.DB_FROM + TABLENAME);// 传过来的参数是 map userUser user = (User) map.get("user");if (user!=null) {sqlBuffer.append(Variable.DB_WHERE + " 1 = 1 ");// 拼接下面的status使用  条件sqlBuffer.append(Variable.DB_AND + " status = " + user.getStatus());}return sqlBuffer.toString(); // 最终拼接好的sql返回出去}
}

在新建一个usermapper 这边使用@param注解 是在sql类下面 使用的是map来取值

import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;
// 配置缓存
@CacheNamespace(size = 512)
public abstract interface UserMapper{/*** 使用注解的方式来调用type下面的数据库操作的方法* * 更新时间 2015年5月18日13:41:21* * 新浪微博 @Steven_LuYang*/// 查询所有的对象 2015年5月18日13:42:11@SelectProvider(type = UserProvider.class, method = "all")@Options(useCache = true, flushCache = false, timeout = 10000) // 请求时间是10秒
// @Results({
//  @Result(id=true,property="userid",column="userid"),
// }) // 如果有什么不一样的 可以写在这个上面  如果都是一样的  那么就不要写public abstract List<User> all();// 根据传过来的user对象查询 2015年5月18日13:45:07@SelectProvider(type = UserProvider.class, method = "list")@Options(useCache = true, flushCache = false, timeout = 10000) // 请求时间是10秒public abstract List<User> list(@Param("user") User user);
}

新建一个service类  这边使用@service注解 是讲这个对象放在spring容器里面

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {@Autowiredprivate UserMapper userMapper;public List<User> all(){return userMapper.all();}}

新建一个controller类  使用controller注解 也是放在springmvc的容器里面 @resquestmapper 使用的是路径 使用boddy注解 是讲这个类作为json格式放回。

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;@Controller()
@RequestMapping("user")
public class UserController {@Autowiredprivate UserService userService;@RequestMapping("all")@ResponseBodypublic List<User> all(){return userService.all();}}

这样一个简单的mvc框架基本就完成了 下面 就是配置文件了 applicationContext 下面配置数据源和 注解要扫描的包

讲这个配置文件放在 web-inf下面

<?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:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:util="http://www.springframework.org/schema/util"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "><context:component-scan base-package="me.note.pro"></context:component-scan><beanclass="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"></bean><!--  配置 BasicDataSource  --><bean id="basicDataSource"class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://127.0.0.1:3310/data"></property><property name="username" value="root"></property><property name="password" value="root"></property><property name="defaultAutoCommit" value="false"></property><!-- configuration the dataSource property --></bean><!--  配置 数据库 事物 --><bean id="dataSourceTransactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="basicDataSource"></property></bean><bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="basicDataSource"></property></bean><!-- 开启事务注解驱动 -->  <tx:annotation-driven />  <!--   <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"><constructor-arg index="0" ref="sqlSessionFactoryBean"></constructor-arg></bean>配置 sqlSessionTemplate  --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  <!--指定要扫描包: 多个包用逗号隔开 -->  <property name="basePackage" value="me.note.pro.mapper" />  <!--指定sqlSessionFactory -->  <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"></property>  </bean> <!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  <property name="sqlSessionFactory" ref="sqlSessionFactoryBean" />  <property name="mapperInterface" value="me.note.pro.mapper.UserMapper" />  </bean> --></beans>

开始使用的是下面的注解 后来看了一下源码 打算写一个类 模仿一下MapperFactoryBean 后来发现上面的父类 可以扫描  就换了一下。

下面是springmvc的配置文件

<?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:p="http://www.springframework.org/schema/p"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-3.0.xsd  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "><!-- 配置包名下的配置文件 --><context:component-scan base-package="me.note.pro"></context:component-scan><beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><property name="messageConverters"><list><bean id="utf8StringHttpMessageConverter"class="me.note.spring.framework.UTF8StringHttpMessageConverter" /><ref bean="mappingJacksonHttpMessageConverter"/></list></property></bean><bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"><property name="supportedMediaTypes"><list><value>text/html;charset=UTF-8</value></list></property></bean></beans>

因为使用spring3.2会有乱码的问题 下面贴一下UTF8StringHttpMessageConverter的源码

转载于:https://my.oschina.net/rightemperor/blog/775239

Spring3.2.0-mybatis3.2.0 基于全注解搭建的后台框架-基础版相关推荐

  1. 基于全注解的SpringMVC+Spring4.2+hibernate4.3框架搭建

    概述 从0到1教你搭建spring+springMVC+hibernate整合框架,基于注解. 本教程框架为基于全注解的SpringMVC+Spring4.2+hibernate4.3,开发工具为my ...

  2. 【万字长文】2022年最全的搭建Web自动化测试框架教程

    测试框架的设计有两种思路,一种是自底向上,从脚本逐步演变完善成框架,这种适合新手了解框架的演变过程.另一种则是自顶向下,直接设计框架结构和选取各种问题的解决方案,这种适合有较多框架事件经验的人.本章和 ...

  3. 基于SOA分布式架构的dubbo框架基础学习篇

    以需求用例为基,抽象接口,Case&Coding两条线并行,服务(M)&消费(VC)分离,单元.接口.功能.集成四层质量管理,自动化集成.测试.交付全程支持. 3个大阶段(需求分析阶段 ...

  4. 前后端分离项目全环境搭建(Ruoyi框架)

    记录一下在全新的电脑上搭建前后端分离项目的全过程,方便下次继续Copy.(以Ruoyi框架为例子进行操作) 目录 前端 1.VsCode 2.NodeJs 后端 JDK idea Tomcat Mav ...

  5. 基于layui和tp5的后台框架hisiphp

    介绍地址:http://www.thinkphp.cn/code/3584.html 演示地址:http://demo.hisiphp.com/admin.php/admin/config/index ...

  6. spring-102-spring全注解快速实现事务

    之前使用jdbc操作数据库,并使用事务的时候是这样操作: Connection connection = null;try {//connection = getConnection(....);// ...

  7. Spring3.2.8+Mybatis3.2.6 多数据源基于BaseDAO的配置

    Spring3.2.8+Mybatis3.2.6 多数据源基于BaseDAO的配置 配置数据源为: MySQL5.5.6 H2Database 1.3.75 这个配置起来比较麻烦,本文这种方法有点麻烦 ...

  8. Spring4.2.6+SpringMVC4.2.6+MyBatis3.4.0 整合

    [0]README 0)本文旨在 review Spring4.2.6+SpringMVC4.2.6+MyBatis3.4.0 整合过程: 1)项目整合所涉及的源代码,please visit  ht ...

  9. 企业如何从 0 到 1 构建整套全链路追踪体系

    简介:本文将分享 ARMS 在全链路追踪领域的最佳实践,分享主要分为四部分.首先,是对分布式链路追踪的整体简介.其次,是对 ARMS 在分布式链路追踪领域的核心能力进行介绍.然后,介绍如何从 0 到 ...

最新文章

  1. APS:大型多模态室内摄像机定位系统
  2. 连接时会提示oracle initialization or shutdown in progress
  3. agentzh 的 Nginx 教程(版本 2015.03.19) 第一篇
  4. 事务控制语句,begin,rollback,savepoint,隐式提交的SQL语句
  5. mysql buffer pool_MySQL的查询缓存和Buffer Pool
  6. 零起点学算法 3个数比较大小
  7. python字符串解释_python基础之字符串详解
  8. mongo-express 远程代码执行漏洞(CVE-2019-10758)
  9. H264视频压缩编码标准简介(二)
  10. CF 799B T-shirt buying
  11. 期货一个价格变动对应价值变化
  12. 对于C# 中事件的参数(object sender, EventArgs e)
  13. 计算机毕业设计java+ssm校友交流论坛(源码+系统+mysql数据库+Lw文档)
  14. QQ登陆界面Resource Hacker制作
  15. Sqlmap命令大全
  16. Webview下载apk
  17. python定时任务启动与停止_python定时任务最强框架APScheduler详细教程
  18. 项目经理之项目经理应该做什么
  19. vlc-for-android
  20. 解决Pycharm中下载不了sklearn问题

热门文章

  1. linux升级ssh到6.6版本,centos6.5升级openssh到7.4版本
  2. 怎么形容智能冰激凌机器人_一种人机交互型冰激凌多功能自动售卖机器人的制作方法...
  3. 福昕pdf编辑器 android,机PDF编辑器安卓/iOS哪家强?职场达人都在用
  4. html wbr标签,HTML wbr标签
  5. java word打印_如何通过Java打印Word文档
  6. 一个中等规模的七段数码数据库以及利用它训练的识别网络
  7. 智能车竞赛技术报告 | 节能信标组 - 中国计量大学 - 赛博 - 8
  8. 无线信标功能调试-2021-3-9-输出功率恒定限制
  9. 无中生有 : 对称方波中的二次谐波
  10. node.js express php,nodejs开发——express路由与中间件