参考博客:http://www.cnblogs.com/magialmoon/archive/2013/10/30/3397828.html
整体结构
POM依赖
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.26</version> </dependency>
数据准备
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
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!-- changes from the defaults for testing --> <setting name="cacheEnabled" value="false" /> <setting name="useGeneratedKeys" value="true" /> <setting name="defaultExecutorType" value="REUSE" /> </settings> <typeAliases> <typeAlias alias="TopData" type="com.cmcc.top.mysql.entity.TopData"></typeAlias> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://172.23.28.57:3306/top?useUnicode=true&characterEncoding=UTF-8" /> <property name="username" value="root" /> <property name="password" value="*****" /> </dataSource> </environment> </environments> <mappers> <mapper resource="sqlMap/TopDataMapper.xml"/> </mappers> </configuration>
TopDataMapper.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="com.cmcc.top.mysql.dao.TopDataDao"> <sql id="tablename">t_migu_top_data</sql> <sql id="Base_Column_List"> music_id, music_name, music_author </sql> <resultMap id="BaseResultMap" type="com.cmcc.top.mysql.entity.TopData"> <result column="music_id" property="musicId" jdbcType="VARCHAR" /> <result column="music_name" property="musicName" jdbcType="VARCHAR" /> <result column="music_author" property="musicAuthor" jdbcType="VARCHAR" /> </resultMap> <insert id="createTopData" useGeneratedKeys="true" keyProperty="id" parameterType="com.cmcc.top.mysql.entity.TopData" > insert into <include refid="tablename" /> ( music_id, music_name, music_author ) values ( #{musicId}, #{musicName}, #{musicAuthor} ) </insert> <insert id="createTopDataByBatch" parameterType="java.util.List" > insert into <include refid="tablename" /> ( music_id, music_name, music_author ) values <foreach collection="list" item="item" index="index" separator="," > ( #{item.musicId}, #{item.musicName}, #{item.musicAuthor} ) </foreach> </insert> <select id="getTopDataList" parameterType="java.util.Map" resultMap="BaseResultMap" > select <include refid="Base_Column_List" /> from <include refid="tablename" /> <where> <if test="musicId !=null " > and music_id = #{musicId} </if> <if test="musicName != null"> and music_name like ‘%${musicName}%‘ </if> <if test="musicAuthor != null"> and music_author like ‘%${musicAuthor}%‘ </if> <if test="musicLanguage != null"> and music_language = #{musicLanguage} </if> <if test="musicStyle != null"> and music_style = #{musicStyle} </if> <if test="startDate != null"> and create_time >= ‘${startDate}‘ </if> <if test="endDate != null"> and ‘${endDate}‘ >= create_time </if> </where> </select> <update id="updateTopData" parameterType="com.cmcc.top.mysql.entity.TopData" > update <include refid="tablename" /> <set> <if test="musicStatus != null"> music_status = #{musicStatus}, </if> <if test="customerFilePath != null"> customer_file_path = #{customerFilePath}, </if> </set> where music_id = #{musicId} </update> </mapper>
实体对象
public class TopData { private String musicId; private String musicName; private String musicAuthor; ... }
接口Dao
public interface TopDataDao { public void createTopData(TopData topData); public void createTopDataByBatch(List<TopData> topDataList); public void updateTopData(TopData topData); //public void updateTopDataByBatch(List<TopData> topDataList); public List<TopData> getTopDataList(Map<String, Object> queryMap); }
manager
public class TopDataManager { private static SqlSession sqlSession; private static TopDataDao topDataDao; public static void initialConnection() { try{ String resource = "mybatis-config.xml"; Reader reader = Resources.getResourceAsReader(resource); SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader); sqlSession = ssf.openSession(); topDataDao = sqlSession.getMapper(TopDataDao.class); } catch (Exception e) { e.printStackTrace(); } } public static void destroyConnection() { sqlSession.close(); } public static void createTopData(TopData topData) { topDataDao.createTopData(topData); sqlSession.commit(); } public static void createTopDataByBatch(List<TopData> topDataList) { topDataDao.createTopDataByBatch(topDataList); sqlSession.commit(); } public static void updateTopData(TopData topData) { topDataDao.updateTopData(topData); sqlSession.commit(); } public static List<TopData> getTopDataList(Map<String, Object> queryMap) { return topDataDao.getTopDataList(queryMap); } }
测试
main 方法访问即可
注意事项
1、mybatis会用log4j记录日志,但是开启debug模式貌似对性能影响非常厉害。
2、mybatis的查询缓存对性能的影响非常大,启用和不启用差距非常大
注意:mapper文件中一定要加上cache这一行,否则不生效。
时间: 2024-11-10 14:13:25