现在公司用的持久层框架mybatis框架,数据库用的mysql数据库,自己已经工作了有一段时间了,感觉在这两方面的技术上有一些自己的体会,特此在这里做一个总结。
查询方面
注意自己的sql的别名写对没有,一般用驼峰命名法来做实体bean的属性命名和数据库的字段名一一对应,我前段时间在想,是不是关于bean的定义是不是应该,按照页面要求的数据来定义,而不是按照数据库的表来编写bean,经过后面的实践发现,我更加侧重于按照页面要求的数据来编写bean。
其次就是在编写<when>和<if>的时候应该注意,and的问题,when后面的第一个条件是不需要and的如果编写上就会报错。还有在做字段判断的时候应该不光是filed!=null在对字符串属性的时候应该加上filed!=null&&filed!="",我开始的时候是在java代码里面做的判断,然后设置成null后来发现直接这样在mapper.xml编写更好,代码更加简洁。
例如
<select id="getMonitorMessageMageList" resultType="AssistMonitorMessage" parameterType="map">
select message.create_time as createTime,
message.name as name,
message.phone as phone,
message.content as content,
message.type as type,
apply.names as boundManInfos
from assist_monitor_message message
LEFT JOIN assist_monitor_apply apply
ON message.apply_id=apply.id
<where>
message.id!=0
<if test="name!=null">
and message.name LIKE '${name}%'
</if>
<if test="phone!=null">
and message.phone=#{phone}
</if>
<if test="timeBeign!=null and timeEnd!=null">
and message.create_time between #{timeBegin} and #{timeEnd}
</if>
LIMIT #{start},#{pageSize};
</where>
</select>
插入方面
后面在做单元测试的时候用了大量的批量插入,总有一点感想吧。
批量插入呢,我开始的时候是写一个简单的sql语句,然后应java 代码循环来做批量插入例如
public void insetData(){
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/qsheal_health_new?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull","root","123456");
String sql="INSERT INTO assist_monitor_apply_device(monitor_apply_id, device_type, device_no,monitor_apply_user_id,parent_device_id) VALUES (?, ?, ?, ?, ?);";
PreparedStatement pst = conn.prepareStatement(sql);
for(int i=0;i<50;i++){
pst.setInt(1,1);
pst.setInt(2, 4);
pst.setString(3, ""+(200+i));
pst.setInt(4, 1);
pst.setInt(5, 1+i);
pst.execute();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这样写,插入的速度慢,而且写的也不规整,但是由于没有用spring的单元测试,直接用的jdbc感觉写起来更加简单,更加方便。可是后来我们领导要求扎入上千条数据的时候感觉就非常的慢了,而且我们没有用spring的单元测试感觉也是十分不对头,后来就变换了方式。
//这两个注解加载类的头上 用于加载spring的配置文件,用上spring的单元测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-core-config.xml")
public class TestAssistBusines {
@Autowired private TestTanService tanService;
@Test
public void insertBusinessBranchDatas(){
//INSERT INTO `assist_business_branch` VALUES (1, '0000001', '份有限公司', '芳', '15478743', 5, '', '2016-8-17 00:43:38', NULL);
List<AssistBusinessBranch> datas = new ArrayList<AssistBusinessBranch>();
for(int i=0;i<1000;i++){
AssistBusinessBranch assistBusinessBranch = new AssistBusinessBranch();
assistBusinessBranch.setCode("0000001");
assistBusinessBranch.setName("四川福怡助老服务管理股份有限公司");
assistBusinessBranch.setChargePerson("刘");
assistBusinessBranch.setChargePhone("15884478743");
assistBusinessBranch.setBid(5);
assistBusinessBranch.setEnabled(true);
assistBusinessBranch.setCreateTime("2016-8-17 00:43:38");
assistBusinessBranch.setEnabledTime("2016-8-17 00:43:38");
datas.add(assistBusinessBranch);
}
Integer len = tanService.insertBusinessBranchDatas(datas);
}
}
mapper.xml文件
<insert id="insetAssistP" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
INSERT INTO `aders`
(`serial_number`, `order_id`, `start_time`, `end_time`, `uid`,
`service_content`, `buid`, `status`, `type`, `bid`, `item_id`, `number`,
`measure_unit_id`,classification,is_pack,pack_id,branch_id)
VALUES
<foreach collection="list" item="item" separator=",">
(#{item.serialNumber}, #{item.orderId}, #{item.startTime},
#{item.endTime},#{item.uid}, #{item.serviceContent},
#{item.buid}, #{item.status}, #{item.type}, #{item.bid},
#{item.itemId}, #{item.number}, #{item.measureUnitId},
#{item.classification},#{item.isPack},#{item.packId},#{item.branchId}
)
</foreach>
</insert>
用mybatis循环value部分的代码做到批量插入,更加方便。
当你要插入的数据来自另一个表的时候应该注意,不要咋爱循环中使用查询语句,这样会让整个插入过程变得很慢,也切记减少插入的次数,但可以增加插入语句的长度
修改update的相关
在整个的项目中关于修改的我其实没有做好多,导致我关于修改方面的东西学到的并不多。
UPDATE assist_monitor_repair `repair`,assist_monitor_apply_device AS adevice SET `repair`.apply_id=adevice.monitor_apply_id
WHERE `repair`.device_no=adevice.device_no
第二种方法
UPDATE product p
INNER JOIN productPrice pp
ON p.productId = pp.productId
SET pp.price = pp.price * 0.8
WHERE p.dateCreated < '2004-01-01'
先将量表做一个连接然后在做更新
这应该算我学到的不多的东西中我感觉比较有用的东西,多表的批量更新。
其实到现在,我感觉在做数据库先关的东西的时候主要是细心。