首页 文章 精选 留言 我的

精选列表

搜索[后台会话],共10000篇文章
优秀的个人博客,低调大师

后台(40)——MyBatis输出映射resultType以及resultMap

探索Android软键盘的疑难杂症 深入探讨Android异步精髓Handler 详解Android主流框架不可或缺的基石 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)— 核心基础 Android多分辨率适配框架(2)— 原理剖析 Android多分辨率适配框架(3)— 使用指南 自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onLayout源码详尽分析 自定义View系列教程04–Draw源码分析及其实践 自定义View系列教程05–示例分析 自定义View系列教程06–详解View的Touch事件处理 自定义View系列教程07–详解ViewGroup分发Touch事件 自定义View系列教程08–滑动冲突的产生及其处理 版权声明 本文原创作者:谷哥的小弟 作者博客地址:http://blog.csdn.net/lfdfhl 我们知道:MyBatis通过resultType对sql的输出参数进行定义,参数的类型可以是:基本类型、HashMap、pojo。在此分别介绍为resultType传入三种类型的不同处理方式。 基本类型 在此,请看一个小例子:统计学生的女同学 先看mapper.xml <select id="countStudent" parameterType="String" resultType='int'> SELECT count(*) from student where gender=#{value} </select> 此处,resultType的类型是int 再来看mapper.java public int countStudent(String string); 最后来看一下测试代码: @Test public void countStudent() throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); int count = studentMapper.countStudent("female"); System.out.println("count="+count); sqlSession.commit(); sqlSession.close(); } HashMap 把resultType的类型指定为hashmap时在执行完sql之后MyBatis将输出的字段名称作为map的key,value为字段值。现在,我们在上个例子的基础上稍加改造。 先看mapper.xml <select id="countStudentByHashMap" parameterType="String" resultType='hashmap'> SELECT count(*) as total from student where gender=#{value} </select> 此处,resultType的类型是hashmap。我们将查询的结果放在total列中 再来看mapper.java public HashMap<String, Object> countStudentByHashMap(String string); 嗯哼,返回的类型是一个HashMap 最后来看一下测试代码: @Test public void countStudentByHashMap() throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); HashMap<String, Object> hashMap = studentMapper.countStudentByHashMap("female"); System.out.println("total="+hashMap.get("total")); sqlSession.commit(); sqlSession.close(); } 我们从查询的结果hashmap中取出key为total对应的值 pojo 我们可将resultType指定为pojo,从而查询出对应的结果。比如,我们可以将resultType的类型指定为Student,从而查询出单个Student或者一个List,在此以查询单个Student为例 先来看mapper.xml <select id="selectStudentByID" parameterType="int" resultType="cn.com.Student"> SELECT * from student where id=#{value} </select> 在此指定resultType的类型是Student 再来看mapper.java public Student selectStudentByID(int id); 最后请看测试 @Test public void selectStudentByID() throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); Student student = studentMapper.selectStudentByID(13); System.out.println(student); sqlSession.commit(); sqlSession.close(); } 在此请注意: 使用resultType进行输出映射时务必将查询的列名与pojo中的属性名保持一致! 看到这里,或许有人会问了:如果如果查询出来的列名和pojo的属性名不一致又怎么办呢?嗯哼,此时最好就不要再用resultType了,可以考虑使用resultMap,请继续往下看 resultMap 为了便于说明,我们来创建一个新的类_Student /** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */ package cn.com; import java.util.Date; public class _Student { private int _id; private String _name; private String _gender; private Date _birthday; public int get_id() { return _id; } public void set_id(int _id) { this._id = _id; } public String get_name() { return _name; } public void set_name(String _name) { this._name = _name; } public String get_gender() { return _gender; } public void set_gender(String _gender) { this._gender = _gender; } public Date get_birthday() { return _birthday; } public void set_birthday(Date _birthday) { this._birthday = _birthday; } @Override public String toString() { return "_Student [_id=" + _id + ", _name=" + _name + ", _gender=" + _gender + ", _birthday=" + _birthday + "]"; } } 首先在mapper.xml中定义一个resultMap <resultMap type="cn.com._Student" id="studentResultMap"> <id column="id" property="_id" /> <result column="name" property="_name" /> <result column="gender" property="_gender" /> <result column="birthday" property="_birthday" /> </resultMap> <resultMap>标签中的type属性表示pojo,请参见代码第1行 <resultMap>标签中的id属性表示resultMap的名字,请参见代码第1行 <id>标签表示数据库表中的主键与pojo的属性的映射关系;比如,此处,将表中的id字段映射为pojo中的_id,请参见代码第2行 <restult>标签表示数据库表中除了主键以外的其他字段与pojo的属性的映射关系,请参见代码第3-5行 然后请看mapper.xml中的SQL语句 <select id="_selectStudentByID" parameterType="int" resultMap="studentResultMap"> SELECT * from student where id=#{value} </select> 此处利用resultMap指定了返回的类型为我们刚才定义的studentResultMap 再来看mapper.java public Student _selectStudentByID(int id); 最后来瞅瞅测试代码 @Test public void _selectStudentByID() throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); _Student _student = studentMapper._selectStudentByID(13); System.out.println(_student); sqlSession.commit(); sqlSession.close(); } 输出结果: _Student [_id=13, _name=大泽玛利亚, _gender=female, _birthday=Thu Mar 16 00:00:00 CST 2017]

优秀的个人博客,低调大师

后台(37)——MyBatis的Mapper开发方式

探索Android软键盘的疑难杂症 深入探讨Android异步精髓Handler 详解Android主流框架不可或缺的基石 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)— 核心基础 Android多分辨率适配框架(2)— 原理剖析 Android多分辨率适配框架(3)— 使用指南 自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onLayout源码详尽分析 自定义View系列教程04–Draw源码分析及其实践 自定义View系列教程05–示例分析 自定义View系列教程06–详解View的Touch事件处理 自定义View系列教程07–详解ViewGroup分发Touch事件 自定义View系列教程08–滑动冲突的产生及其处理 版权声明 本文原创作者:谷哥的小弟 作者博客地址:http://blog.csdn.net/lfdfhl 使用Mybatis开发Dao,通常有两个方法:原始Dao开发方式和Mapper接口开发方式。 在本篇文章中,我们在前两篇博客的基础上来一起完成Mapper接口开发方式。 开发规范 Mapper接口开发方式比原始的DAO的方式要简便许多,但是这种简便是建立在规范之上的,所以在采用该方式时务必严格遵守开发规范. 在Mapper接口开发方式中有两个核心的东西:mapper.xml和mapper.java mapper接口开发需要遵循以下规范: 1、mapper.xml文件中的namespace与mapper.java接口的类的全路径相同。 2、mapper.java接口中的方法名和mapper.xml中定义的每个sql的id相同 3、mapper.java接口中的方法的输入参数类型和mapper.xml中定义的每个sql的parameterType的类型保持一致 4、mapper.java接口中方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型保持一致 好了,我们现在就按照此规范来改造之前的例子 StudentMapper.java /** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */ package cn.com; import java.util.List; public interface StudentMapper { public Student findStudentById(int id); public List<Student> findStudentByName(String name); public void insertStudent(Student student); public void deleteStudent(int id); public void updateStudent(Student student); } StudentMapper.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="cn.com.StudentMapper"> <select id="findStudentById" parameterType="int" resultType="cn.com.Student"> SELECT * FROM student WHERE id=#{value} </select> <select id="findStudentByName" parameterType="java.lang.String" resultType="cn.com.Student"> SELECT * FROM student WHERE name LIKE '%${value}%' </select> <insert id="insertStudent" parameterType="cn.com.Student"> <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> SELECT LAST_INSERT_ID() </selectKey> INSERT INTO student (name,gender,birthday) value (#{name},#{gender},#{birthday}) </insert> <delete id="deleteStudent" parameterType="java.lang.Integer"> DELETE FROM student where id=#{id} </delete> <update id="updateStudent" parameterType="cn.com.Student"> UPDATE student set name=#{name},gender=#{gender},birthday=#{birthday} where id=#{id} </update> </mapper> 嗯哼,对照着这两个文件看就会发现:我们在书写的过程中严格遵守了开发规范。 TestCRUD.java /** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */ package cn.com; import java.io.IOException; import java.io.InputStream; import java.util.Date; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Before; import org.junit.Test; public class TestCRUD { private SqlSessionFactory sqlSessionFactory; @Before public void intiSqlSessionFactory() throws Exception { String resource = "SqlMapConfig.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } @Test public void findStudentById() throws IOException{ SqlSession sqlSession=sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); Student student = studentMapper.findStudentById(5); System.out.println(student); } @Test public void findStudentByName() throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); List<Student> list = studentMapper.findStudentByName("木"); for (Student student : list) { System.out.println(student); } } @Test public void insertStudent() throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); Student student=new Student(); student.setName("小小木希"); student.setGender("female"); student.setBirthday(new Date()); studentMapper.insertStudent(student); sqlSession.commit(); sqlSession.close(); System.out.println(student.getId()); } @Test public void deleteStudent() throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); studentMapper.deleteStudent(5); sqlSession.commit(); sqlSession.close(); } @Test public void updateStudent() throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); Student student=new Student(); student.setId(5); student.setName("空空姐姐"); student.setGender("female"); student.setBirthday(new Date()); studentMapper.updateStudent(student); sqlSession.commit(); sqlSession.close(); } } 这些测试用例中,最重要的就是: StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); 得到Mapper,再调用它定义的增删改查方法 最后,按照惯例还是附上项目的结构图:

优秀的个人博客,低调大师

后台(20)——数据库连接池

探索Android软键盘的疑难杂症 深入探讨Android异步精髓Handler 详解Android主流框架不可或缺的基石 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)— 核心基础 Android多分辨率适配框架(2)— 原理剖析 Android多分辨率适配框架(3)— 使用指南 自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onLayout源码详尽分析 自定义View系列教程04–Draw源码分析及其实践 自定义View系列教程05–示例分析 自定义View系列教程06–详解View的Touch事件处理 自定义View系列教程07–详解ViewGroup分发Touch事件 自定义View系列教程08–滑动冲突的产生及其处理 版权声明 本文原创作者:谷哥的小弟 作者博客地址:http://blog.csdn.net/lfdfhl 数据库连接池简介 在前面的文章中我们已经介绍了Web开发和数据库。现在来想这么一个问题:在同一时间段有大量用户访问我们的服务端,那么此时的服务器数据库它忙得过来么?诚然,它是需要一个好帮手的——数据库连接池 数据库连接池负责分配、管理和释放数据库连接。它允许程序重复使用一个现有的数据库连接,而不是再重新建立一个。数据库连接池可自动释放闲置时间超过最大空闲时间的数据库连接从而避免因为没有释放数据库连接而引起的数据库连接遗漏。这些技术均能明显提高数据库操作性能。目前,常见的数据库连接池有DBCP、C3P0等,现分别介绍他们。 DBCP DBCP(DataBase Connection Pool)由Apache研发,而且Tomcat的连接池也正是采用DBCP实现的,该数据库连接池既可与应用服务器整合使用,也可由应用程序独立使用。 在此,以完整示例介绍DBCP的使用 第一步:添加jar包 commons-dbcp.jar commons-pool.jar mysql-connector-java-5.0.8-bin.jar 第二步:编写DBCP的配置文件dbcpconfig.properties #<!-- 连接设置 --> driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/db1 username=root password=root #<!-- 初始化连接 --> initialSize=10 #<!-- 最大连接数量 --> maxActive=50 #<!-- 最大空闲连接 --> maxIdle=20 #<!-- 最小空闲连接 --> minIdle=5 #<!-- 超时等待时间(单位毫秒) --> maxWait=50000 #<!-- 编码方式 --> connectionProperties=useUnicode=true;characterEncoding=utf8 ##<!-- 指定由连接池所创建的连接自动提交 --> defaultAutoCommit=true #<!-- 指定由连接池所创建的连接的事务级别 --> defaultTransactionIsolation=REPEATABLE_READ 第三步:编写操作DBCP的工具类DBCPUtil /** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */ package cn.com; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSourceFactory; public class DBCPUtil { private static DataSource dataSource = null; //创建数据库连接池 static{ Properties properties = new Properties(); try { ClassLoader classLoader=DBCPUtil.class.getClassLoader(); properties.load(classLoader.getResourceAsStream("dbcpconfig.properties")); dataSource = BasicDataSourceFactory.createDataSource(properties); } catch (Exception e) { throw new ExceptionInInitializerError("DBCP始化错误,请检查配置文件"); } } //创建连接 public static Connection getConnection(){ try { return dataSource.getConnection(); } catch (SQLException e) { throw new RuntimeException("数据库连接错误"); } } //释放连接 public static void releaseConnection(Connection conn, Statement stmt, ResultSet rs) { if (rs != null) { try { rs.close(); } catch (Exception e) { e.printStackTrace(); } rs = null; } if (stmt != null) { try { stmt.close(); } catch (Exception e) { e.printStackTrace(); } stmt = null; } if (conn != null) { try { conn.close(); } catch (Exception e) { e.printStackTrace(); } conn = null; } } } 第四步:测试DBCP的使用 /** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */ package cn.com; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.junit.Test; public class TestDBCP { @Test public void testDBCP(){ Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet=null; try { connection = DBCPUtil.getConnection(); preparedStatement = connection.prepareStatement("select * from student"); resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { Student student = new Student(); int id = resultSet.getInt("studentid"); String name = resultSet.getString("studentname"); student.setStudentID(id); student.setStudentName(name); System.out.println(student); } } catch (SQLException e) { e.printStackTrace(); }finally{ DBCPUtil.releaseConnection(connection, preparedStatement, resultSet); } } } 运行结果如下图所示: C3P0 C3P0是一个开源的JDBC连接池,目前有Hibernate,Spring等框架也使用该数据库连接池。 在此,以完整示例介绍C3P0的使用 第一步:添加jar包 c3p0-0.9.1.2.jar mysql-connector-java-5.0.8-bin.jar 第二步:编写C3P0的配置文件c3p0-config.xml <?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/db1</property> <property name="user">root</property> <property name="password">root</property> <property name="initialPoolSize">15</property> <property name="maxIdleTime">40</property> <property name="maxPoolSize">150</property> <property name="minPoolSize">20</property> </default-config> </c3p0-config> 第三步:编写操作C3P0的工具类C3P0Util /** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */ package cn.com; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class C3P0Util { //创建数据库连接池 private static DataSource dataSource = new ComboPooledDataSource(); //创建连接 public static Connection getConnection(){ try { return dataSource.getConnection(); } catch (SQLException e) { throw new RuntimeException("获取数据库连接失败"); } } //释放连接 public static void releaseConnection(Connection conn, Statement stmt, ResultSet rs) { if (rs != null) { try { rs.close(); } catch (Exception e) { e.printStackTrace(); } rs = null; } if (stmt != null) { try { stmt.close(); } catch (Exception e) { e.printStackTrace(); } stmt = null; } if (conn != null) { try { conn.close(); } catch (Exception e) { e.printStackTrace(); } conn = null; } } } 第四步:测试C3P0的使用 /** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */ package cn.com; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.junit.Test; public class TestC3P0 { @Test public void testC3P0(){ Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet=null; try { connection = C3P0Util.getConnection(); preparedStatement = connection.prepareStatement("select * from student"); resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { Student student = new Student(); int id = resultSet.getInt("studentid"); String name = resultSet.getString("studentname"); student.setStudentID(id); student.setStudentName(name); System.out.println(student); } } catch (SQLException e) { e.printStackTrace(); }finally{ C3P0Util.releaseConnection(connection, preparedStatement, resultSet); } } } 运行结果如下图所示:

优秀的个人博客,低调大师

MineAdmin v1.2.0 发布,权限管理后台框架

⭐ 新功能及优化 [新增] 代码生成器添加tag页配置方式及选项 [新增] 添加迁移回滚命令 mine:migrate-rollback --name=模块名 [新增] 新增数据源功能,可以在代码生成器载入远程数据库的表结构到本地库 [新增] 新增获取每日必应背景图 [优化] 优化excel导出支持超过26列 [优化] 抛出的异常全部允许跨域 🐞 BUG修复 修复Auth注解只获取method参数的,未获取class的bug

优秀的个人博客,低调大师

MineAdmin v1.1.2 发布,权限管理后台框架

📈 统计 目前版本有效代码行数源码代码行数(不包含注释和空行):6979 ⭐ 新功能及优化 [新增] 附件列表无权限验证接口 [新增] 代码生成条件增加in和not in [新增] mapper基方法paramsEmptyQuery(),emptyBuildQuery()感谢@NEKGod贡献的代码 [更新] api文档接口增加分组数据,接口按分组来显示 [更新] 所有hyperf组件到最新版本 [优化] 多模块按order排序,避免初始化安装系统时,先安装自定义模块 感谢@裘牧贡献的代码 🐞 BUG修复 修复古老时期因使用雪花id造成队列消息的一个小bug 修复应用未绑定某接口也可以访问的bug

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册