参考博客:http://www.cnblogs.com/magialmoon/archive/2013/10/30/3397828.html
整体结构
POM依赖
org.mybatis
mybatis
3.2.3
mysql
mysql-connector-java
5.1.26
数据准备
CREATE TABLE `t_migu_top_data` (
`music_id` int(11) DEFAULT NULL,
`music_name` varchar(100) DEFAULT NULL,
`music_author` varchar(100) DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
配置文件
mybatis-config.xml
TopDataMapper.xml
t_migu_top_data
music_id,
music_name,
music_author
insert into(
music_id,
music_name,
music_author
)
values
(
#{musicId},
#{musicName},
#{musicAuthor}
)
insert into(
music_id,
music_name,
music_author
)
values(
#{item.musicId},
#{item.musicName},
#{item.musicAuthor}
)
selectfrom
and music_id = #{musicId}
and music_name like '%${musicName}%'
and music_author like '%${musicAuthor}%'
and music_language = #{musicLanguage}
and music_style = #{musicStyle}
and create_time >= '${startDate}'
and '${endDate}' >= create_time
update
music_status = #{musicStatus},
customer_file_path = #{customerFilePath},
where music_id = #{musicId}
实体对象
public classTopData {privateString musicId;privateString musicName;privateString musicAuthor;
...
}
接口Dao
public interfaceTopDataDao {public voidcreateTopData(TopData topData);public void createTopDataByBatch(ListtopDataList);public voidupdateTopData(TopData topData);//public void updateTopDataByBatch(List topDataList);
public List getTopDataList(MapqueryMap);
}
manager
public classTopDataManager {private staticSqlSession sqlSession;private staticTopDataDao topDataDao;public static voidinitialConnection() {try{
String resource= "mybatis-config.xml";
Reader reader=Resources.getResourceAsReader(resource);
SqlSessionFactory ssf= newSqlSessionFactoryBuilder().build(reader);
sqlSession=ssf.openSession();
topDataDao= sqlSession.getMapper(TopDataDao.class);
}catch(Exception e) {
e.printStackTrace();
}
}public static voiddestroyConnection() {
sqlSession.close();
}public static voidcreateTopData(TopData topData) {
topDataDao.createTopData(topData);
sqlSession.commit();
}public static void createTopDataByBatch(ListtopDataList) {
topDataDao.createTopDataByBatch(topDataList);
sqlSession.commit();
}public static voidupdateTopData(TopData topData) {
topDataDao.updateTopData(topData);
sqlSession.commit();
}public static List getTopDataList(MapqueryMap) {returntopDataDao.getTopDataList(queryMap);
}
}
测试
main 方法访问即可
注意事项
1、mybatis会用log4j记录日志,但是开启debug模式貌似对性能影响非常厉害。
2、mybatis的查询缓存对性能的影响非常大,启用和不启用差距非常大
注意:mapper文件中一定要加上cache这一行,否则不生效。