publicclassClient{ publicstaticvoidmain(String[] args){ Customer customer = new Customer(); Order order = new DeliveryGuy(customer); order.order(); } }
int ret = sqlSession.getMapper(UserMapper.class).deleteUser(5); System.out.println(ret); sqlSession.commit(); sqlSession.close(); }
@Test publicvoidtest_update(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); User user = new User(2, "change", "pwdchange"); int ret = sqlSession.getMapper(UserMapper.class).updateUser(user); System.out.println(ret); sqlSession.commit(); sqlSession.close(); }
@Test publicvoidtest_getUserById(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); User ret = sqlSession.getMapper(UserMapper.class).getUserById(1); System.out.println(ret); sqlSession.close(); } }
org.apache.ibatis.binding.BindingException: Type interface com.jzheng.dao.UserDao is not known to the MapperRegistry.
-- 核心配置文件没有配置 mapper 路径
Caused by: java.io.IOException: Could not find resource com/jzheng/dao/UserMapper.xml at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:114) at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:100) at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:372) at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:119) ... 27 more
-- maven 约定大于配置,默认指挥将 resources 下面的 xml 导出到 target, 如果需要将 java 下的配置文件到处需要再 pom.xml 下的 build tag 里加点配置
Map<String, Object> map = new HashMap<>(); map.put("id", 1); User ret = sqlSession.getMapper(UserMapper.class).getUserByMap(map); System.out.println(ret); sqlSession.close(); }
<selectid="getTeacher"resultMap="TeacherStudent"> select s.id sid, s.name sname, t.name tname, t.id tid from student s, teacher t where s.tid = t.id and t.id=#{tid}; </select>
<selectid="queryBlogIf"parameterType="map"resultType="blog"> select * from mybatis.blog where 1=1 <iftest="title != null"> and title=#{title} </if> <iftest="author != null"> and author=#{author} </if> </select>
choose (when, otherwise), 这种判断语句更贴近 java 中的 switch-case,在 if 中,所有符合 test 判断的条件都会被添加进去,但是在 choose 中,他只会从众多条件中选择一种
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<selectid="queryBlogChoose"parameterType="map"resultType="blog"> select * from mybatis.blog <where> <choose> <whentest="title != null"> title = #{title} </when> <whentest="author != null"> and author = #{author} </when> <otherwise> and views = #{views} </otherwise> </choose> </where> </select>
trim (where, set), where 可以对 xml 中定义的 and + where 冗余情况进行判断,只在需要的时候才添加这个关键字,同理 set 会处理 set + ,的情况
<sqlid="if-title-author"> <iftest="title != null"> and title=#{title} </if> <iftest="author != null"> and author=#{author} </if> </sql>
<selectid="queryBlogIf"parameterType="map"resultType="blog"> select * from mybatis.blog <where> <includerefid="if-title-author"></include> </where> </select>
最好基于单表来定义 SQL 片段
不要存在 where 标签
Cache 缓存 - mybatis-07-cache
在 DB 操作中连接数据库是非常消耗资源的,所以有了缓存机制来减少重复的查询操作消耗
缓存:一次查询的结果,给他暂存在内存中,再次查询的时候直接走取结果
一级缓存
一级缓存默认开启,且不能关闭,只在一次 SqlSession 中有用
开启日志
测试一次 session 中查询两次相同结果
查看日志输出
缓存失效的几种情况:
查询不同的东西
增删改可能会改变原来的数据,所以必定要刷新缓存
查询不同的 mapper.xml
手动清理缓存
测试 p1
1 2 3 4 5 6 7 8 9 10 11 12
@Test publicvoidgetUsers(){ SqlSession session = MybatisUtils.getSqlSession(); System.out.println("-----> query user1 the first time <-----"); session.getMapper(UserMapper.class).getUserById(1); System.out.println("-----> query user1 the second time <-----"); session.getMapper(UserMapper.class).getUserById(1); System.out.println("-----> query user2 the second time <-----"); session.getMapper(UserMapper.class).getUserById(2);
session.close(); }
输出 log
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
-----> query user1 the first time <----- Opening JDBC Connection Created connection 1866875501. Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6f46426d] ==> Preparing: select * from mybatis.user where id=?; ==> Parameters: 1(Integer) <== Columns: id, name, pwd <== Row: 1, jack, 123 <== Total: 1 -----> query user1 the second time <----- -----> query user2 the second time <----- ==> Preparing: select * from mybatis.user where id=?; ==> Parameters: 2(Integer) <== Columns: id, name, pwd <== Row: 2, change, pwdchange <== Total: 1 Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6f46426d] Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@6f46426d] Returned connection 1866875501 to pool.
@Test publicvoidgetUsers(){ SqlSession session = MybatisUtils.getSqlSession(); System.out.println("-----> query user1 the first time <-----"); session.getMapper(UserMapper.class).getUserById(1); session.clearCache(); // 手动清 cache !!! System.out.println("-----> query user1 the second time <-----"); session.getMapper(UserMapper.class).getUserById(1); session.close(); }
输出 log
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
-----> query user1 the first time <----- Opening JDBC Connection Created connection 1936722816. Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@73700b80] ==> Preparing: select * from mybatis.user where id=?; ==> Parameters: 1(Integer) <== Columns: id, name, pwd <== Row: 1, jack, 123 <== Total: 1 -----> query user1 the second time <----- ==> Preparing: select * from mybatis.user where id=?; ==> Parameters: 1(Integer) <== Columns: id, name, pwd <== Row: 1, jack, 123 <== Total: 1
@Test publicvoidgetUsers_diff_session(){ SqlSession session1 = MybatisUtils.getSqlSession(); System.out.println("-----> query user1 the first time <-----"); session1.getMapper(UserMapper.class).getUserById(1); session1.close();
SqlSession session2 = MybatisUtils.getSqlSession(); System.out.println("-----> query user1 the second time <-----"); session2.getMapper(UserMapper.class).getUserById(1); session2.close(); }
当 mapper 中没有添加 标签时,输出如下,两个 session 查询同一个 user 的时候都进行了 DB 访问
-----> query user1 the first time <----- Opening JDBC Connection Created connection 1936722816. Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@73700b80] ==> Preparing: select * from mybatis.user where id=?; ==> Parameters: 1(Integer) <== Columns: id, name, pwd <== Row: 1, jack, 123 <== Total: 1 Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@73700b80] Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@73700b80] Returned connection 1936722816 to pool. -----> query user1 the second time <----- Opening JDBC Connection Checked out connection 1936722816 from pool. Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@73700b80] ==> Preparing: select * from mybatis.user where id=?; ==> Parameters: 1(Integer) <== Columns: id, name, pwd <== Row: 1, jack, 123 <== Total: 1 Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@73700b80]
当 mapper 中添加 标签时,输出如下,第二次查询 user 时是从 cache 中查找的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
-----> query user1 the first time <----- Cache Hit Ratio [com.jzheng.mapper.UserMapper]: 0.0 Opening JDBC Connection Created connection 379645464. Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@16a0ee18] ==> Preparing: select * from mybatis.user where id=?; ==> Parameters: 1(Integer) <== Columns: id, name, pwd <== Row: 1, jack, 123 <== Total: 1 Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@16a0ee18] Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@16a0ee18] Returned connection 379645464 to pool. -----> query user1 the second time <----- Cache Hit Ratio [com.jzheng.mapper.UserMapper]: 0.5
Idea 链接 mysql 后报错 Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' property manually,可以通过设置 mysql 时区解决
cmd -> mysql -uroot -p 登录 DB
show variables like'%time_zone'; 查看时区, Value 为 SYSTEM 则表示没有设置过
set global time_zone = '+8:00'; 修改时区为东八区
重试链接,问题解决
这只是临时方案,重启 DB 后时区会重置,可以去 my.ini 配置文件中添加配置
1 2 3
[mysqld] # 设置默认时区 default-time_zone='+8:00'
MacOS 版本安装
1
brew install mysql # 使用 homebrew 安装
安装完毕的时候,终端回给出提示,最后的那段话比较值得注意
1 2 3 4 5 6 7 8 9 10 11 12
We've installed your MySQL database without a root password. To secure it run: mysql_secure_installation
MySQL is configured to only allow connections from localhost by default
To connect run: mysql -uroot
To have launchd start mysql now and restart at login: brew services start mysql Or, if you don't want/need a background service you can just run: mysql.server start
翻译成人话就是
DB 安装成功,但是数据库 root 用户是没有密码的,你直接登陆会失败
运行 mysql_secure_installation 给数据库设置密码
使用命令 mysql -uroot 联接数据库
后台启动使用 brew services start mysql 前台启动使用 mysql.server start
VALIDATE PASSWORD COMPONENT can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD component?
## 是否进行安全设置 Press y|Y for Yes, any other key for No: y
There are three levels of password validation policy:
LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
## 设置密码复杂度,最低也要 *8* 位密码起步 Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0 Please set the password for root here.
New password:
Re-enter new password:
Estimated strength of the password: 50 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.
## 是否删除匿名用户 Remove anonymous users? (Press y|Y for Yes, any other key for No) : y Success.
Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.
## 是否开放 root 用户远程访问 Disallow root login remotely? (Press y|Y for Yes, any other key for No) :
... skipping. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.
## 是否删除测试表 Remove test database and access to it? (Press y|Y for Yes, any other key for No) :
... skipping. Reloading the privilege tables will ensure that all changes made so far will take effect immediately.
## 是否重新加载使得配置生效 Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y Success.
All done!
设置完毕之后就可以使用 mysql -uroot -p 登陆测试了。没有遇到其他问题,还挺顺利的 ε-(´∀`; )
# 创建 100 条数据只显示 5% < index < 95% | makeresults count=100 | eval random_num=random() | streamstats count as index | eventstats max(index) as m_idx | where index > m_idx*0.05 AND index < m_idx*0.95
Locate the .tar.gz file you just downloaded, and then click Open or Choose.
Click Upload.
Click Restart Splunk, and then confirm that you want to restart.
To install apps and add-ons directly into Splunk Enterprise Put the downloaded file in the $SPLUNK_HOME/etc/apps directory. Untar and ungzip your app or add-on, using a tool like tar -xvf (on *nix) or WinZip (on Windows). Restart Splunk.
After you install a Splunk app, you will find it on Splunk Home. If you have questions or need more information, see Manage app and add-on objects.
三个小例子快速入门
搜索 event 并通过饼图展示
输入时间节点和关键词:MessageBox topic=com.successfactors.usermanagement.event.UserChangeEvent | stats count by servername
// 客户端调用 publicclassClient{ publicstaticvoidmain(String[] args){ Book book = new Book(); Shoes shoes = new Shoes(); PostageVisitor postageVisitor = new PostageVisitor();
// class version 52.0 (52) // access flags 0x21 public class sorra/tracesonar/mytest/SayHello implements sorra/tracesonar/mytest/MyInterface01 sorra/tracesonar/mytest/MyInterface02 {