Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件。 1、相关文件 关于Mybatis-Generator的下载可以到这个地址:https://github.com/mybatis/generator/releases 由于我使用的是Mysql数据库,这里需要在准备一个连接mysql数据库的驱动jar包 以下是相关文件截图: 和Hibernate逆向生成一样,这里也需要一个配置文件: generatorConfig.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?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> <!--数据库驱动--> <classPathEntry location="mysql-connector-java-5.0.8-bin.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库链接地址账号密码--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/mymessages" userId="root" password="root"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--生成Model类存放位置--> <javaModelGenerator targetPackage="lcw.model" targetProject="src"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--生成映射文件存放位置--> <sqlMapGenerator targetPackage="lcw.mapping" targetProject="src"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!--生成Dao类存放位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="lcw.dao" targetProject="src"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!--生成对应表及类名--> <table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration> |
需要修改文件配置的地方我都已经把注释标注出来了,这里的相关路径(如数据库驱动包,生成对应的相关文件位置可以自定义)不能带有中文。 上面配置文件中的:
1 |
<table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> |
tableName和domainObjectName为必选项,分别代表数据库表名和生成的实力类名,其余的可以自定义去选择(一般情况下均为false)。 生成语句文件:
1 |
java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite |
2、使用方法 在该目录按住Shift键,右键鼠标选择"在此处打开命令窗口",复制粘贴生成语句的文件代码即可。 看下效果图: 生成相关代码: Message.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package lcw.model; public class Messgae { private Integer id; private String title; private String describe; private String content; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title == null ? null : title.trim(); } public String getDescribe() { return describe; } public void setDescribe(String describe) { this.describe = describe == null ? null : describe.trim(); } public String getContent() { return content; } public void setContent(String content) { this.content = content == null ? null : content.trim(); } } |
MessgaeMapper.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
<?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="lcw.dao.MessgaeMapper" > <resultMap id="BaseResultMap" type="lcw.model.Messgae" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="title" property="title" jdbcType="VARCHAR" /> <result column="describe" property="describe" jdbcType="VARCHAR" /> <result column="content" property="content" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List" > id, title, describe, content </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from message where id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from message where id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="lcw.model.Messgae" > insert into message (id, title, describe, content) values (#{id,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, #{describe,jdbcType=VARCHAR}, #{content,jdbcType=VARCHAR}) </insert> <insert id="insertSelective" parameterType="lcw.model.Messgae" > insert into message <trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > id, </if> <if test="title != null" > title, </if> <if test="describe != null" > describe, </if> <if test="content != null" > content, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="id != null" > #{id,jdbcType=INTEGER}, </if> <if test="title != null" > #{title,jdbcType=VARCHAR}, </if> <if test="describe != null" > #{describe,jdbcType=VARCHAR}, </if> <if test="content != null" > #{content,jdbcType=VARCHAR}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="lcw.model.Messgae" > update message <set > <if test="title != null" > title = #{title,jdbcType=VARCHAR}, </if> <if test="describe != null" > describe = #{describe,jdbcType=VARCHAR}, </if> <if test="content != null" > content = #{content,jdbcType=VARCHAR}, </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="lcw.model.Messgae" > update message set title = #{title,jdbcType=VARCHAR}, describe = #{describe,jdbcType=VARCHAR}, content = #{content,jdbcType=VARCHAR} where id = #{id,jdbcType=INTEGER} </update> </mapper> |
MessgaeMapper.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package lcw.dao; import lcw.model.Messgae; public interface MessgaeMapper { int deleteByPrimaryKey(Integer id); int insert(Messgae record); int insertSelective(Messgae record); Messgae selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(Messgae record); int updateByPrimaryKey(Messgae record); } |
from:https://www.cnblogs.com/zhujiabin/p/4990762.html
View Details这两天启动了一个新项目因为项目组成员一直都使用的是 Mybatis,虽然个人比较喜欢 Jpa 这种极简的模式,但是为了项目保持统一性技术选型还是定了 Mybatis 。到网上找了一下关于 Spring Boot 和 Mybatis 组合的相关资料,各种各样的形式都有,看的人心累,结合了 Mybatis 的官方 Demo 和文档终于找到了最简的两种模式,花了一天时间总结后分享出来。 Orm 框架的本质是简化编程中操作数据库的编码,发展到现在基本上就剩两家了,一个是宣称可以不用写一句 Sql 的 Hibernate,一个是可以灵活调试动态 Sql 的 Mybatis ,两者各有特点,在企业级系统开发中可以根据需求灵活使用。发现一个有趣的现象:传统企业大都喜欢使用 Hibernate ,互联网行业通常使用 Mybatis 。 Hibernate 特点就是所有的 Sql 都用 Java 代码来生成,不用跳出程序去写(看) Sql ,有着编程的完整性,发展到最顶端就是 Spring Data Jpa 这种模式了,基本上根据方法名就可以生成对应的 Sql 了,有不太了解的可以看我的上篇文章Spring Boot(五): Spring Data Jpa 的使用。 Mybatis 初期使用比较麻烦,需要各种配置文件、实体类、Dao 层映射关联、还有一大推其它配置。当然 Mybatis 也发现了这种弊端,初期开发了generator可以根据表结果自动生产实体类、配置文件和 Dao 层代码,可以减轻一部分开发量;后期也进行了大量的优化可以使用注解了,自动管理 Dao 层和配置文件等,发展到最顶端就是今天要讲的这种模式了,mybatis-spring-boot-starter 就是 Spring Boot+ Mybatis 可以完全注解不用配置文件,也可以简单配置轻松上手。 现在想想 Spring Boot 就是牛逼呀,任何东西只要关联到 Spring Boot 都是化繁为简。 mybatis-spring-boot-starter 官方说明:MyBatis Spring-Boot-Starter will help you use MyBatis with Spring Boot 其实就是 Mybatis 看 Spring Boot 这么火热也开发出一套解决方案来凑凑热闹,但这一凑确实解决了很多问题,使用起来确实顺畅了许多。mybatis-spring-boot-starter主要有两种解决方案,一种是使用注解解决一切问题,一种是简化后的老传统。 当然任何模式都需要首先引入mybatis-spring-boot-starter的 Pom 文件,现在最新版本是 2.0.0 […]
View DetailsNET Core 应用程序相对于以前的.NET Framework 应用程序在启动运行的方式上有一定的差异,今天就来谈一谈这个获取应用程序启动路径的问题。 1.工作路径 WorkingDirectory 下面的两种方式都可以获取工作路径,结果都是一样的:
1 2 3 |
Environment.CurrentDirectory; Directory.GetCurrentDirectory(); |
其实所谓的工作路径就是我们应用程序的启动路径,所以我们平时所说的获取应用程序的启动路径,也是通过上面的方式。 (1)我们通过VS F5直接运行 VS会先编译我们的项目,输出到Debug\对应的sdk版本 目录下,然后以这个目录作为工作路径,启动我们的应用程序。 (2)通过dotnet 命令运行 我们在项目根目录,执行 dotnet run命令: 我们执行 dotnet run命令来启动时,对于程序的工作路径就是执行命令的路径,所以说,获取到的路径变化了。但是我们通过dotnet run命令运行的应用程序文件实际所在的目录也是和上面的目录一样的,即:Debug\对应的sdk版本,我们可以通过代码来测试一下: 新加的代码是获取程序集所在的路径,可以发现也是在 Debug\对应的sdk版本 目录下的。 我们将程序发布到 D:\test 目录下 可以看到,前两种方式获取到的都是执行dotnet命令所在的目录即工作目录,后一种方式是获取到的我们应用程序所在的目录。 2.结论 通过上面的测试,我们可以得出结论,.NET Core 应用程序获取工作路径/启动路径,就是获取的执行dotnet命令时所在的目录,所以当我们在Linux等系统部署时,设置守护进程时,记得一定要将工作路径设置为程序文件所在的目录,不然应用程序获取到的路径将不会是应用程序文件所在的目录,当我们在应用程序里设置了一些相对路径,诸如读取配置文件,写日志(Log4net、NLog),将会与我们的预期不一样。因为相对路径,是默认相对于应用程序的工作路径的。
1 2 3 4 5 |
Environment.CurrentDirectory; <span class="hljs-comment">//获取应用程序工作目录</span> Directory.GetCurrentDirectory();<span class="hljs-comment">//获取应用程序工作目录(和上面的方式效果是一样的)</span> Path.GetDirectoryName(<span class="hljs-keyword">typeof</span>(Program).Assembly.Location);<span class="hljs-comment">//获取应用程序所在目录(绝对,不受工作目录影响)</span> |
AppContext.BaseDirectory 也可以获取应用程序所在目录 from:https://www.cnblogs.com/stulzq/p/9220502.html
View Details第一步先注册session: 在Startup.cs文件中的ConfigureServices方法中添加:
1 |
services.AddSession(); |
在Startup.cs文件中的Configure方法中加上这一句代码
1 |
app.UseSession(); |
第二步从nuget安装Microsoft.AspNetCore.Mvc引用,直接使用自带的方法进行设置和获取session 不过自带的方法设置和获取的session值是byte[]类型的,可以从nuget安装并引用Microsoft.AspNetCore.Http并使用里面的扩展方法
1 |
using Microsoft.AspNetCore.Http; |
扩展后的方法为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
/// <summary> /// 设置Session /// </summary> /// <param name="key">键</param> /// <param name="value">值</param> protected void SetSession(string key, string value) { HttpContext.Session.SetString(key, value); } /// <summary> /// 获取Session /// </summary> /// <param name="key">键</param> /// <returns>返回对应的值</returns> protected string GetSession(string key) { var value = HttpContext.Session.GetString(key); if (string.IsNullOrEmpty(value)) value = string.Empty; return value; } |
下面这个图片里面的方法是自带的操作session的方法 下面这个图片是扩展的方法: from:https://www.cnblogs.com/dawenyang/p/9227713.html
View Details您有一个Web API,并且想要实现自己的授权逻辑,该怎么做?您需要做四件事。 1. 创建您的自定义授权属性 2. 在控制器上使用自定义授权属性 3. 在自定义请求管道中间件中创建授权逻辑 4. 启动时注册中间件 创建您的自定义授权属性
1 2 3 4 5 6 7 8 9 10 |
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)] public class CustomAuthorizeAttribute : Attribute { public string[] AllowedUserRoles { get; private set; } public CustomAuthorizeAttribute(params string[] allowedUserRoles) { this.AllowedUserRoles = allowedUserRoles; } } |
在控制器上使用自定义授权属性
1 2 3 4 5 6 7 8 9 10 11 12 |
[ApiController] [Route("[controller]")] public class WeatherForecastController : ControllerBase { [HttpGet] [CustomAuthorize("Admin", "Supervisor", "Worker")] public string Get() { return "Sunny"; } } |
在自定义请求管道中间件中创建授权逻辑
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
public static class CustomAuthorizationMiddleware { public static async Task Authorize(HttpContext httpContext, Func<dynamic> next) { var endpointMetaData = httpContext.GetEndpoint().Metadata; bool hasCustomAuthorizeAttribute = endpointMetaData.Any(x => x is CustomAuthorizeAttribute); if (!hasCustomAuthorizeAttribute) { await next.Invoke(); return; } CustomAuthorizeAttribute customAuthorizeAttribute = endpointMetaData .FirstOrDefault(x => x is CustomAuthorizeAttribute) as CustomAuthorizeAttribute; // TODO: change authorization logic bool isAuthorized = customAuthorizeAttribute.AllowedUserRoles .Any(allowedRole => httpContext.User.IsInRole(allowedRole)); if (isAuthorized) { await next.Invoke(); return; } httpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized; await httpContext.Response.WriteAsync("unauthorized"); } } |
启动时注册中间件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseHttpsRedirection(); app.UseRouting(); app.Use(CustomAuthorizationMiddleware.Authorize); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } |
确保在调用app.UseRouting()之后添加中间件。这样可以确保在将路由 信息添加到HttpContext 后执行您的中间件。 from:https://www.cnblogs.com/bisslot/p/12330985.html
View Details配置gradle镜像 在 %USERPROFILE%\.gradle下面创建新文件init.gradle,输入下面的内容并保存
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
allprojects{ repositories { def REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public/' all { ArtifactRepository repo -> if(repo instanceof MavenArtifactRepository){ def url = repo.url.toString() if (url.startsWith('https://repo1.maven.org/maven2') || url.startsWith('https://jcenter.bintray.com/')) { project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL." remove repo } } } maven { url REPOSITORY_URL } } } |
配置maven镜像 复制E:\maven\apache-maven-3.3.9\conf中的settings.xml到%USERPROFILE%\.m2中,修改mirrors即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<mirrors> <!-- mirror | Specifies a repository mirror site to use instead of a given repository. The repository that | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used | for inheritance and direct lookup purposes, and must be unique across the set of mirrors. | <mirror> <id>mirrorId</id> <mirrorOf>repositoryId</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://my.repository.com/repo/path</url> </mirror> --> <mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> </mirror> <mirror> <id>jcenter</id> <mirrorOf>central</mirrorOf> <name>jcenter.bintray.com</name> <url>http://jcenter.bintray.com/</url> </mirror> <mirror> <id>repo1</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://repo1.maven.org/maven2/</url> </mirror> <mirror> <id>repo2</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://repo2.maven.org/maven2/</url> </mirror> </mirrors> |
build.gradle 配置
1 2 3 4 5 |
repositories { mavenLocal() mavenCentral() maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'} } |
作者:liurongming 链接:https://www.jianshu.com/p/67b547b4e6ae 来源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
View DetailsMariaDB 10.4.12 Stable# Row size too large (> 8126). Changing some columns to TEXT or BLOB may help.# 以下两步解决问题 1. 修改my.ini文件在[mysqld]下面加入下面三行#
1 2 3 |
innodb_file_per_table innodb_file_format = Barracuda innodb_strict_mode = 0 |
2. 新建一个查询进行如下操作将nombre_tabla改成你的表名#
1 2 3 |
ALTER TABLE nombre_tabla ENGINE=InnoDB ROW_FORMAT=COMPRESSED; |
from:https://www.cnblogs.com/fanlinglong/p/12425116.html
View Details一、引入starter
1 2 |
//热部署 compile("org.springframework.boot:spring-boot-devtools") |
二、开启自动编译 第一步
1 2 3 |
windows:ctrl + alt + shift + / mac: command + alt + shift + / |
弹出以下界面 第二步 点击Registry,勾选compiler.automake.allow.when.app.running 第三步 勾选 Make/Build project automatically 重新运行项目,随意改一个java类,看看项目是不是重启了^_^ from:https://blog.csdn.net/JonWu0102/article/details/81064776
View Details在引用依赖时经常会有这样的问题:某些间接引用的依赖项是不需要的;产生了依赖冲突。此时需要排除一些依赖。 下面的内容介绍了几种在gradle中排除依赖的方式。 在dependency中排除 1 2 3 4 5 6 7 8 dependencies { compile('com.zhyea:ar4j:1.0') { //excluding a particular transitive dependency: exclude module: 'cglib' //by artifact name exclude group: 'org.jmock' //by group exclude group: 'org.unwanted', module: 'iAmBuggy' //by both name and group } } 这种方式是粒度最细的,也是最为繁琐的。此时可以考虑全局设置。 在全局配置中排除 全局配置是在configuration中完成的。 1 2 3 4 configurations { compile.exclude module: 'cglib' all*.exclude group:’org.unwanted', module: 'iAmBuggy' } 禁用传递依赖 禁用传递依赖需要将属性transitive设置为false。可以在dependency中禁用传递依赖,也可以在configuration中全局禁用: 1 2 3 4 5 6 7 compile('com.zhyea:ar4j:1.0') { transitive = false } configurations.all { transitive = false } 还可以在单个依赖项中使用@jar标识符忽略传递依赖: […]
View Detailsspringboot 中默认的web容器是tomcat。 在maven 的pom 文件中加入如下依赖,便可使用tomcat 容器。
1 2 3 4 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> |
如果想使用 jetty 作为 web容器,需要2步操作: 1.排除默认的tomcat 容器
1 2 3 4 5 6 7 8 9 10 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> |
2.加入 jetty 的 starter 依赖
1 2 3 4 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> |
from:https://www.cnblogs.com/appleat/p/9100822.html
View Details