关于Mybatis的一些随笔
Mapper.xml头文件
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
Mybatis配置头文件
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
Mybatis配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!-- 设置resultMap的自动映射级别 --> <setting name="autoMappingBehavior" value="FULL"/> <!-- 设置全局性延迟加载 --> <setting name="lazyLoadingEnabled" value="false"/> </settings> <!-- 设置实体类别名 --> <typeAliases> <package name="cn.xin.pojo" /> </typeAliases> </configuration>
Sql映射文件
<?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="cn.xin.dao.user.UserMapper"> <select id="queryLogin" resultType="User"> select * from smbms_user where userCode=#{userCode} </select> <select id="userCount" resultType="int"> select count(*) from smbms_user,smbms_role where smbms_user.userRole=smbms_role.id <if test="userName!=null and userName!=''"> and smbms_user.userName=#{userName} </if> <if test="roleName!=null and roleName!=''"> and smbms_role.roleName=#{roleName} </if> </select> <select id="getUserList" resultMap="userlist"> select * from smbms_user,smbms_role where smbms_user.userRole=smbms_role.id <trim suffixOverrides="and"> <if test="userName!=null and userName!=''"> and smbms_user.userName=#{userName} </if> <if test="roleName!=null and roleName!=''"> and smbms_role.roleName=#{roleName} </if> </trim> order by smbms_user.id limit #{from},#{pageSize} </select> <resultMap type="UserList" id="userlist"> <id property="id" column="id"/> <result property="userCode" column="userCode"/> <association property="role" javaType="Role"> <id property="id" column="r_id"/> <result property="roleName" column="roleName"/> </association> </resultMap> <insert id="addUser" parameterType="User"> insert into smbms_user(userCode,userName,userPassword,gender,birthday,phone,address,userRole,idPicPath,workPicPath) value(#{userCode},#{userName},#{userPassword},#{gender},#{birthday},#{phone},#{address},#{userRole},#{idPicPath},#{workPicPath}) </insert> <select id="getUserById" resultType="User"> select * from smbms_user where id=#{id}; </select> <update id="modifyUserById" parameterType="User"> update smbms_user set userName=#{userName},gender=#{gender},birthday=#{birthday},phone=#{phone},address=#{address},userRole=#{userRole} where id=#{id} </update> <select id="queryUserById" parameterType="int" resultType="User"> select * from smbms_user where id=#{id} </select> <delete id="delUserById" parameterType="int"> delete from smbms_user where id=#{id} </delete> <select id="getUserByuserCode" > select * from smbms_user where userCode=#{userCode} </select> </mapper>
逆向工程配置头文件
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
逆向工程generatorConfig.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <context id="testTables" targetRuntime="Mybatis3"> <commentGenerator> <!-- 是否去除自动生成的注释true、flase --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- 数据库连接的信息:驱动类、连接地址、用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/smbms" userId="root" password="174173315"> </jdbcConnection> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC类型解析为Integer,为true时解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- targetProject:生成PO类的位置 --> <javaModelGenerator targetPackage="cn.xin.pojo" targetProject=".\src"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="trimSubPackges" value="false"/> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimString" value="true"/> </javaModelGenerator> <!-- targetProject:mapper映射文件生成的位置 --> <sqlMapGenerator targetPackage="cn.xin.dao" targetProject=".\src"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- targetPackage:mapper接口生成的位置 --> <javaClientGenerator targetPackage="cn.xin.dao" type="XMLMAPPER" targetProject=".\src"> <!-- 是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!-- 指定数据库表 --> <table tableName="smbms_user"></table> </context> </generatorConfiguration>
逆向工程生成类
package cn.com.generator.main; import java.io.File; import java.util.ArrayList; import java.util.List; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.api.ShellCallback; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; public class GeneratorSqlMap { public void generator() throws Exception{ //warnings为用于放置生成过程中警告信息的集合对象 List<String> warnings = new ArrayList<String>(); //指定DefaultShellCallback是否覆盖重名文件 boolean overwrite = true; //加载配置文件 File configFile = new File("generatorConfig.xml"); //配置解析类 ConfigurationParser cp = new ConfigurationParser(warnings); //配置解析类解析配置文件并生成Configuration配置对象 Configuration config = cp.parseConfiguration(configFile); //DefaultShellCallback负责如何处理重复文件 ShellCallback callback = new DefaultShellCallback(overwrite); //逆向工程对象 MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); //执行逆向文件生成操作 myBatisGenerator.generate(null); } public static void main(String[] args) throws Exception { GeneratorSqlMap generatorSqlmap = new GeneratorSqlMap(); generatorSqlmap.generator(); } }
Mybatis随笔
1.多参数入参: Mapper接口参数:(@Param("id")Integer id,@Param("password")String pwd);Mapper.xml无需填写parameterType2.@Param注解: Mybatis的参数类型为Map,使用@Param注解的参数,会记录指定的参数名为key;否则默认为"param"+序号作为Map的key,这种情况可能会引起sql语句中获取不到#{参数名},从而报错3.resultMap的作用: 描述数据库结果集合对象的对应关系,属性名与字段名不同的匹配、association处理一对一关联关系、collection处理一对多关联关系、自动映射级别:NONE:禁止自动匹配,PARTIAL(默认):自动匹配所有属性,有内部嵌套的除外,FULL:自动匹配所有属性,包括内部嵌套4.动态SQL: if+where、if+trim、if+set、foreach、choose(when/otherwise)、where 1=1、分页:oreder by id limit #{start}},#{count}(查询结果根据id排序,从start开始,每次显示5个查询结果) foreach:迭代对象为数组,collection=array、迭代对象为List,collection=list、迭代对象为Map,collection=key
select * from tb_member where id in <foreach collection="array" item="id" open="(" separator="," close=")">#{id}</foreach>
关于Mybatis的一些随笔的更多相关文章
- Mybatis 逆向工程学习随笔
一.逆向工程的作用 简单来说,就是替我们生成Java代码. 之前使用Mybatis的Mapper代理方法开发,还需要自己创建实体类,而且属性还得和数据库中的字段对应.这着实是机械化的而且比较麻烦的事, ...
- 2018-01-08 学习随笔 SpirngBoot整合Mybatis进行主从数据库的动态切换,以及一些数据库层面和分布式事物的解决方案
先大概介绍一下主从数据库是什么?其实就是两个或N个数据库,一个或几个主负责写(当然也可以读),另一个或几个从只负责读.从数据库要记录主数据库的具体url以及BigLOG(二进制日志文件)的参数.原理就 ...
- springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)
这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...
- mybatis随笔二之SqlSessionFactory
在上一篇文章我们已经得到了DefaultSqlSessionFactory @Override public SqlSession openSession() { return openSession ...
- mybatis随笔一之SqlSessionFactoryBuilder
SqlSessionFactoryBuilder是构建sqlSessionFactory的入口类 从该类的方法可知,它是通过不同的入参来构造SqlSessionFactory,除了最后一个config ...
- MyBatis 从浅入深 随笔整理
MyBatis? archetypeCatalog = internal 本文档单独出现的_parameter都标识为变量名 一.三个基本要素: 核心接口和类 MyBatis 核心配置文件 SQL映射 ...
- Mybatis学习随笔
学习Mybatis路径(适合有java基础和mysql基础的小伙伴) 1.把项目搭建起来,跑一跑感受一下 2.测试基本映射 3.测试高级映射 4.测试动态sql 5.学习懒加载与缓存 6.与sprin ...
- MyBatis随笔
前一阵参与了一个项目的搭建,为了快速开发再加上学一些新东西,准备采用React+Spring MVC+MyBatis的架构. 花了一些时间最终把Spring MVC+MyBatis打通. 这里总结下M ...
- mybatis逆向工程随笔
mybatis框架简介和简单原理: mybatis本来是apache的一个开源的项目,后来迁移到了google,并且改名为mybatis. mybatis框架优点: 1.mybatis是最简单的持久化 ...
随机推荐
- win10装ubuntu双系统
由于在win下进行web开发出现各种问题相当头疼. 所以今天折腾了一天想装个ubuntu,查看了网上好多教程,不得不说,网上的人很多都是不负责任的,教程都是过时根本就不负责任,关键的地方一笔带过,简单 ...
- python访问mysql
1,下载mysql-connector-python-2.0.4 pythoin访问mysql需要有客户端,这个就是连接mysql的库 解压后如下图: 双击lib 以windows为例 把mysql ...
- 设计模式之策略模式(Strategy Pattern)
模板方法是通过继承实现的,在父类中定义出算法的骨架,将不同点在子类中实现.而策略模式是通过接口实现的,策略中定义了完整的算法.它们有点像啊-- 策略模式的定义 策略模式(Strategy Patter ...
- springboot+mybatis+ehcache实现缓存数据
一.springboot缓存简介 在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManager),Spring Boot根据下面的顺序去侦测缓 ...
- sql中count(*)、count(col)、count(1)区别
count(*)和count(列)根本就是不等价的,count(*)是针对于全表的,而count(列)是针对于某一列的,如果此列值为空的话,count(列)是不会统计这一行的. 也就是说count(列 ...
- 对混合数值,字符,null的字段进行排序
今天有个需求是进行排序. 这一列值是字符串类型的, 但是里面有数值型 比如"1" 和null类型的. 实现效果是需要 数值型的先按照数值的方式先排,然后字符串按照字符传排,最后 ...
- HttpClient 专题
HttpClient is a HTTP/1.1 compliant HTTP agent implementation based on HttpCore. It also provides reu ...
- memcache 和 redis 之间的区别
结论 应该说Memcached和Redis都能很好的满足解决我们的问题,它们性能都很高,总的来说,可以把Redis理解为是对Memcached的拓展,是更加重量级的实现,提供了更多更强大的功能.具体来 ...
- 测试网页时需要添加等待的情形 (Selenium)
测试网页时需要添加等待的情形: 1. 网页跳转 2. DOM结构发生改变.
- float_array.go
) if err != nil { log.Fatalf("Could not parse: %s", s) ret ...