第二周工作笔记

第二周工作笔记


img

mybatis-plus的安装与配置

随着系统开发的深入,数据处理层维护的sql语句越来越多,且大部分时间都在重复相同的操作,因此使用mybatis-plus取代mybatis进行dao层的开发与维护。,mybatis-plus基于mybatis进行扩展,因此不需要改动之前的sql语句,可以在新的持久层框架下进行sql的操作。

mybatis-plus的安装

在pom.xml中添加相关的依赖

1
2
3
4
5
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.1.2</version>
</dependency>

同时还要取消掉原来 mybatis和mybatis-spring的依赖防止冲突。项目框架中还使用了mybatis-ehcache,保险起见也取消其依赖。同时在 spring-datasource.xml取消其bean设置。

mybatis-plus的配置

打开spring-mybatis进行以下配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!-- SqlSessionFactory Config -->
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:com/project/qcc/mapper/*Mapper.xml"/>
<property name="typeAliasesPackage" value="com.project.qcc.entity" />
<property name="plugins">
<array>
<bean class="com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor"></bean>
<bean class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"></bean>
</array>
</property>
</bean>
<!-- MyBatis Mapper Scan Config -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.project.qcc.dao"/>
</bean>

至此配置完成。

mybatis-plus的使用

相关类的继承

在实体类使用注解声明实体类对应的表名

1
@Tablename("name")

在实体类使用注解声明表的主键

1
@TableId

使dao层接口继承 mybatis-plus的 baseMapper

1
public interface UserInfoMapper extends BaseMapper<UserInfo>{}

使用baseMapper封装好的CRUD方法进行dao层操作
api使用手册

使service层接口继承 mybatis-plus的 IService

1
public interface UserService extends IService<UserInfo>{}

使serviceImpl继承 mybatis-plus的 ServiceImpl

1
public class UserServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserService {}

使用IService封装好的CRUD方法进行Service层操作
api使用手册

在Controller中编写测试类

测试类

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
@RequestMapping(value="/test")
public Object v_test(){
//查询记录总数
int count = userDao.selectCount(null);
System.out.println(count);
//查询全部记录
List<UserInfo> userInfo = userDao.selectList(null);
userInfo.forEach(System.out::println);
//使用id更新字段
UserInfo userInfo2 = userDao.selectById("10005");
System.out.println(userInfo2.toString());
userInfo2.setEmail("1036192040@qq.com");
//使用id更新email
int res = userDao.updateById(userInfo2);
System.out.println("成功结果数:"+res);
//使用service层封装方法进行 根据id进行批量更新
UserInfo userInfo1 = userService.getById("10001");
UserInfo userInfo21 = userService.getById("10003");
List<UserInfo> list = new ArrayList<UserInfo>();
userInfo1.setEmail("test1");
userInfo21.setEmail("test2");
list.add(userInfo1);
list.add(userInfo21);
boolean isSuccess = userService.updateBatchById(list);
System.out.println("更新:"+isSuccess);
return true;
}

输出结果

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
Time:17 ms - ID:com.project.qcc.dao.UserInfoMapper.selectCount
Execute SQL:com.mchange.v2.c3p0.impl.NewProxyPreparedStatement@72db4ef7
26
Time:15 ms - ID:com.project.qcc.dao.UserInfoMapper.selectList
Execute SQL:com.mchange.v2.c3p0.impl.NewProxyPreparedStatement@15e1681a
UserInfo [userid=10000, loginname=admin, password=3d4f2bf07dc1be38b20cd6e46949a1071f9d0e3d, cjsj=2019-07-15 08:54:59.0, xgsj=2019-07-15 08:54:59.0, username=admin, roleName=, status=1, manage=3]
UserInfo [userid=10001, loginname=admin11, password=3d4f2bf07dc1be38b20cd6e46949a1071f9d0e3d, cjsj=2019-07-15 09:45:54.0, xgsj=2019-07-16 09:15:16.0, username=无, roleName=, status=1, manage=1]
UserInfo [userid=10002, loginname=zhaochong, password=fa376e383626491fb6f3b6b5c06b1c208bba702b, cjsj=2019-07-15 08:55:07.0, xgsj=2019-07-15 10:59:38.0, username=无, roleName=, status=1, manage=3]
UserInfo [userid=10003, loginname=admin111, password=011c945f30ce2cbafc452f39840f025693339c42, cjsj=2019-07-15 09:43:08.0, xgsj=2019-07-16 09:15:16.0, username=无, roleName=, status=1, manage=1]
UserInfo [userid=10004, loginname=wodemaya, password=3d4f2bf07dc1be38b20cd6e46949a1071f9d0e3d, cjsj=2019-07-15 09:43:08.0, xgsj=2019-07-16 09:15:16.0, username=无, roleName=, status=1, manage=1]
UserInfo [userid=10005, loginname=admin1111, password=011c945f30ce2cbafc452f39840f025693339c42, cjsj=2019-07-15 09:43:08.0, xgsj=2019-07-16 09:15:16.0, username=无, roleName=, status=1, manage=1]
UserInfo [userid=10006, loginname=admin11111, password=011c945f30ce2cbafc452f39840f025693339c42, cjsj=2019-07-15 09:43:08.0, xgsj=2019-07-16 09:15:16.0, username=无, roleName=, status=1, manage=1]
UserInfo [userid=10007, loginname=zhaochong1, password=011c945f30ce2cbafc452f39840f025693339c42, cjsj=2019-07-15 09:43:08.0, xgsj=2019-07-16 09:15:16.0, username=无, roleName=, status=1, manage=1]
UserInfo [userid=10008, loginname=sadasd, password=3d4f2bf07dc1be38b20cd6e46949a1071f9d0e3d, cjsj=2019-07-15 09:43:08.0, xgsj=2019-07-16 09:15:16.0, username=无, roleName=, status=1, manage=1]
UserInfo [userid=10009, loginname=zhaochong122, password=011c945f30ce2cbafc452f39840f025693339c42, cjsj=2019-07-15 09:43:08.0, xgsj=2019-07-16 09:15:16.0, username=无, roleName=, status=1, manage=1]
UserInfo [userid=10010, loginname=zhaochong122, password=011c945f30ce2cbafc452f39840f025693339c42, cjsj=2019-07-15 09:43:08.0, xgsj=2019-07-16 09:15:16.0, username=无, roleName=, status=1, manage=1]
UserInfo [userid=10011, loginname=123123, password=7110eda4d09e062aa5e4a390b0a572ac0d2c0220, cjsj=2019-07-15 09:43:08.0, xgsj=2019-07-16 09:15:16.0, username=无, roleName=, status=1, manage=1]
UserInfo [userid=10012, loginname=adminw, password=3d4f2bf07dc1be38b20cd6e46949a1071f9d0e3d, cjsj=2019-07-15 09:46:05.0, xgsj=2019-07-16 09:15:16.0, username=无, roleName=, status=1, manage=1]
UserInfo [userid=10013, loginname=admin2, password=3d4f2bf07dc1be38b20cd6e46949a1071f9d0e3d, cjsj=2019-07-15 09:22:04.0, xgsj=2019-07-16 09:15:16.0, username=无, roleName=, status=1, manage=1]
UserInfo [userid=10014, loginname=sssss, password=3d4f2bf07dc1be38b20cd6e46949a1071f9d0e3d, cjsj=2019-07-15 09:22:03.0, xgsj=2019-07-16 09:15:16.0, username=无, roleName=, status=1, manage=1]
UserInfo [userid=10015, loginname=admin23, password=011c945f30ce2cbafc452f39840f025693339c42, cjsj=2019-07-15 08:47:51.0, xgsj=2019-07-16 09:15:16.0, username=无, roleName=, status=1, manage=1]
UserInfo [userid=10016, loginname=12312311, password=78f8bb4c43c7c3e4e5883e8e9b18518c89d965ff, cjsj=2019-07-15 09:22:01.0, xgsj=2019-07-16 09:15:16.0, username=无, roleName=, status=1, manage=1]
UserInfo [userid=10017, loginname=zhaochong11, password=011c945f30ce2cbafc452f39840f025693339c42, cjsj=2019-07-15 09:21:58.0, xgsj=2019-07-16 09:15:16.0, username=无, roleName=, status=1, manage=1]
UserInfo [userid=10018, loginname=xxxxx, password=7b21848ac9af35be0ddb2d6b9fc3851934db8420, cjsj=2019-07-15 09:21:54.0, xgsj=2019-07-16 09:15:16.0, username=无, roleName=, status=1, manage=1]
UserInfo [userid=10019, loginname=as11111, password=7b21848ac9af35be0ddb2d6b9fc3851934db8420, cjsj=2019-07-15 09:21:56.0, xgsj=2019-07-16 09:15:16.0, username=无, roleName=, status=1, manage=1]
UserInfo [userid=10020, loginname=sssssssss, password=7b21848ac9af35be0ddb2d6b9fc3851934db8420, cjsj=2019-07-15 09:21:52.0, xgsj=2019-07-16 09:15:16.0, username=无, roleName=, status=1, manage=1]
UserInfo [userid=10022, loginname=zhaochong333, password=011c945f30ce2cbafc452f39840f025693339c42, cjsj=2019-07-23 09:48:35.0, xgsj=2019-07-16 09:15:16.0, username=无, roleName=, status=1, manage=1]
UserInfo [userid=10024, loginname=123123ss, password=011c945f30ce2cbafc452f39840f025693339c42, cjsj=2019-07-15 10:03:45.0, xgsj=2019-07-16 09:15:16.0, username=无, roleName=, status=1, manage=1]
UserInfo [userid=10025, loginname=test, password=3d4f2bf07dc1be38b20cd6e46949a1071f9d0e3d, cjsj=2019-07-15 10:58:29.0, xgsj=2019-07-16 09:15:16.0, username=无, roleName=, status=1, manage=1]
UserInfo [userid=10026, loginname=zhaochong123, password=011c945f30ce2cbafc452f39840f025693339c42, cjsj=2019-07-15 15:07:03.0, xgsj=2019-07-16 09:15:16.0, username=无, roleName=, status=1, manage=1]
UserInfo [userid=10027, loginname=adminsdsd, password=7b21848ac9af35be0ddb2d6b9fc3851934db8420, cjsj=2019-07-15 18:00:19.0, xgsj=2019-07-16 09:15:16.0, username=无, roleName=, status=1, manage=1]
Time:5 ms - ID:com.project.qcc.dao.UserInfoMapper.selectById
Execute SQL:com.mchange.v2.c3p0.impl.NewProxyPreparedStatement@81c43dd
UserInfo [userid=10005, loginname=admin1111, password=011c945f30ce2cbafc452f39840f025693339c42, cjsj=2019-07-15 09:43:08.0, xgsj=2019-07-16 09:15:16.0, username=无, roleName=, status=1, manage=1]
Time:7 ms - ID:com.project.qcc.dao.UserInfoMapper.updateById
Execute SQL:com.mchange.v2.c3p0.impl.NewProxyPreparedStatement@42aba9c6
成功结果数:1
Time:3 ms - ID:com.project.qcc.dao.UserInfoMapper.selectById
Execute SQL:com.mchange.v2.c3p0.impl.NewProxyPreparedStatement@6cafa6fd
Time:4 ms - ID:com.project.qcc.dao.UserInfoMapper.selectById
Execute SQL:com.mchange.v2.c3p0.impl.NewProxyPreparedStatement@739e71ec
Time:0 ms - ID:com.project.qcc.dao.UserInfoMapper.updateById
Execute SQL:com.mchange.v2.c3p0.impl.NewProxyPreparedStatement@53c58db
Time:0 ms - ID:com.project.qcc.dao.UserInfoMapper.updateById
Execute SQL:com.mchange.v2.c3p0.impl.NewProxyPreparedStatement@53c58db
更新:true

ajax传递数组

前端需要加入

1
jQuery.ajaxSettings.traditional = true;

后端在传入参数中设置与ajax上传的同名参数

1
2
3
4
$.post("url",{name:name},function(data){
})
public Object test(String []name){}

前端解析json

需要后台返回 JsonObject 在前端使用JSON.parse解析

不使用submit按钮进行jqery验证

1
$("#formid").valid();

mybatis-plus使用注意

mybatis-plus中的baseMapper接口 在有两个以上接口类继承它时 编译器会报 required a single bean, but 2 were found 的错误。
研究别人项目的配置文件时发现几乎所有的spring版本都在4.0以上,故进行了spring的升级,可以正常运行。

×

纯属好玩

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

文章目录
  1. 1. mybatis-plus的安装与配置
    1. 1.1. mybatis-plus的安装
    2. 1.2. mybatis-plus的配置
    3. 1.3. mybatis-plus的使用
      1. 1.3.1. 相关类的继承
      2. 1.3.2. 在Controller中编写测试类
  2. 2. ajax传递数组
  3. 3. 前端解析json
  4. 4. 不使用submit按钮进行jqery验证
  5. 5. mybatis-plus使用注意