使用mybatis的话,每个Dao就对于一个相应的xml文件,我来给个例子个大家看,先要配置好环境。在application.xml里面
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<property name="initialPoolSize" value="${jdbc.initialPoolSize}" />
<property name="minPoolSize" value="${jdbc.minPoolSize}" />
<property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
<property name="numHelperThreads" value="https://cdn.jxasp.com:9143/image/20" />
<property name="maxStatements" value="100" />
<property name="maxIdleTime" value="3600" />
<property name="acquireIncrement" value="https://cdn.jxasp.com:9143/image/2" />
<property name="acquireRetryAttempts" value="5" />
<property name="acquireRetryDelay" value="600" />
<property name="testConnectionOnCheckin" value="true" />
<property name="idleConnectionTestPeriod" value="1200" />
<property name="checkoutTimeout" value="10000" />
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="annotationClass" value="org.springframework.stereotype.Repository" />
<property name="basePackage" value="com.shishuo.studio.dao" />
然后我在dao层写个类
package com.shishuo.studio.dao;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import com.shishuo.studio.entity.TagSkill;
import com.shishuo.studio.entity.vo.TagSkillVo;
public interface TagSkillDao {
public int addTagSkill(TagSkill tagSkill);
public int deleteTagSkill(@Param("skillId") long skillId);
public int updateNameBySkillId(@Param("skillId") long skillId,
@Param("name") String name);
public TagSkillVo getTagSkillByTagId(@Param("skillId") long skillId);
public TagSkillVo getTagSkillByName(@Param("name") String name);
public List<TagSkillVo> getTagSkillListByUserId(@Param("userId") long userId);
然后再就是对应的XML文件 TagSkill.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.shishuo.studio.dao.TagSkillDao">
<insert id="addTagSkill" parameterType="com.shishuo.studio.entity.TagSkill">
(name,content,status,createTime)
(#{name},#{content},#{status},#{createTime})
<selectKey resultType="long" keyProperty="skillId">
<delete id="deleteTagSkill" parameterType="Long">
delete from tag_skill where skillId=#{skillId}
<update id="updateNameBySkillId">
update tag_skill set name=#{name} where skillId=#{skillId}
<select id="getTagSkillByTagId" parameterType="Long"
resultType="com.shishuo.studio.entity.vo.TagSkillVo">
select * from tag_skill where skillId=#{skillId}
<select id="getTagSkillByName"
resultType="com.shishuo.studio.entity.vo.TagSkillVo">
select * from tag_skill where name=#{name}
<select id="getTagSkillListByUserId"
resultType="com.shishuo.studio.entity.vo.TagSkillVo">
SELECT tags.skillId,tags.name,tags.content,tags.status,tags.createTime FROM shishuo.tag_skill tags,shishuo.teacher_skill teas where tags.skillId=teas.tagSkillId and userId=#{userId};
使用经验