Mybatis Mysql

-----------------------------------

XML:

 <insert id="addMerchantShopEmployee" parameterType="com.enterprise.domain.MerchantShopEmployee">

INSERT INTO b_merchant_shop_employee(user_name,merchant_id,is_admin,nick_name,emp_name,dept,email,telephone,mer_shop_ids,mer_user_id,user_id) 

VALUES(#{userName},#{merchantId},#{isAdmin},#{nickName},#{empName},#{dept},#{email},#{telephone},#{merShopIds},#{merUserId},#{userId})

</insert>

 

<insert id="addHyipUserRole" parameterType="java.util.HashMap">

INSERT INTO hyip_user_role(username,user_role)VALUES(#{username},#{userRole})

</insert>

 

<delete id="deleteUserRole" parameterType="java.util.HashMap">

delete from b_merchat_user_role_rel where user_id = #{userId}

</delete>

 

<delete id="removeHyipUserRole" parameterType="java.lang.String">

delete FROMhyip_user_role

WHERE

user_role IN (

SELECT

priv_name

FROM

b_merchant_priviledge

)

AND username = #{username}

</delete>

 

<update id="modfifyEmployeeState" parameterType="java.util.HashMap">

update b_merchant_shop_employee set state=#{state}  where user_id=#{userId}

</update>

 

<update id="modfifyEmployeeBasicInfo" parameterType="java.util.HashMap">

update b_merchant_shop_employee  set email=#{email},emp_name=#{empName},

nick_name=#{nickName}, telephone=#{telephone} where user_id=#{userId}

</update>

 

<select id="qryMerchantShopEmployee" parameterType="java.util.HashMap" resultType="com.enterprise.domain.MerchantShopEmployee">

select * from  b_merchant_shop_employee where 1=1

<if test="merchantUserId !=null and merchantUserId != ''">

and mer_user_id=#{merchantUserId} 

</if>

limit #{perPage} offset #{offset} 

 

</select>

 

<select id="getMerchanCompanyCertifiedFrom"  resultType="MerchantCompanyCertified" parameterType="String">

SELECT 

 * 

FROM

 hyip_merchant_company_certified 

WHERE create_time >= STR_TO_DATE(

   #{from},

   '%Y-%m-%d%k:%i:%s'

 ) 

 OR update_time >= STR_TO_DATE(

   #{from},

   '%Y-%m-%d%k:%i:%s'

 )

</select>

 

 

 

--------------------------------------

JAVA

 

interface definition:

@MyBatisRepository

public interface MerchantCompanyCertifiedRepository {

void addMerchanCompanyCertified(MerchantCompanyCertified shopInfo);

 

void updateMerchanCompanyCertified(MerchantCompanyCertified shopInfo);

 

MerchantCompanyCertified getMerchanCompanyCertified(long userId);

 

List<MerchantCompanyCertified> getMerchanCompanyCertifiedFrom(String from);

 

}

 

传参数:

@Override

public void updateMerchanCompanyCertified(MerchantCompanyCertified shopInfo) {

merchantCompanyCertifiedRepository.updateMerchanCompanyCertified(shopInfo);

 

}

 

Map<String,Object> param = page.loadFilter();

param.put("companyName", companyName);

param.put("rowNum", page.loadIDisplayLength());

param.put("skipNum", page.loadIDisplayStart());

int count = merchantCompanyShopInfoService.getMerchantShopViewCount(param);

   List<MerchantShopView> shopList = merchantCompanyShopInfoService.getMerchantShopViewList(param);

 

 

 

 

 

--------------------------------

分页

分页需要和前端开发沟通如何传参

后台必须返回前台:

     1  总的记录数

     2  根据当前页+pageSize 所查询出来的记录

 

 java

 List<MerchantShopEmployee> employeeList = merchantInfoService.qryMerchantShopEmployee(merUserId, perPage, currentPage);

int countSize = merchantInfoService.countEmployee(merUserId);

 

 map.put("data", Collections.EMPTY_LIST);

map.put("totalCount", countSize);

return AjaxResult.success(map);

 

 

 @Override

public List<MerchantShopEmployee> qryMerchantShopEmployee(Long merchantUserId, Integer perPage, Integer currentPage) {

Map<String, Object> condtion = new HashMap<String, Object>();

if (merchantUserId != null) {

condtion.put("merchantUserId", merchantUserId.toString());

}

condtion.put("perPage", perPage);

               condtion.put("offset", (currentPage-1)  * perPage);

 

List<MerchantShopEmployee> employeeList = merchantemployeeRepository.qryMerchantShopEmployee(condtion);

return employeeList;

}

 

mybatis XML:

<select id="qryMerchantShopEmployee" parameterType="java.util.HashMap" resultType="com.enterprise.domain.MerchantShopEmployee">

select * from  b_merchant_shop_employee where 1=1

<if test="merchantUserId !=null and merchantUserId != ''">

and mer_user_id=#{merchantUserId} 

</if>

limit #{perPage} offset #{offset} 

 

</select>

 

<select id="countEmployee" resultType="int">

select count(*) from (

select * from  b_merchant_shop_employee where 1=1

<if test="merchantUserId !=null and merchantUserId!=''">

and mer_user_id=#{merchantUserId} 

</if>

) a      

 

</select>

Count引入临时表的好处是查询临时表的sql只要能执行正确,count(*)就能正确执行,而查询临时表的sql通常在mybatis.xml中已经有了

 

------------------------------------------

 

 一个错误

引用参数的时候忘记用#{}引起来

### SQL: INSERT INTO b_merchat_user_role_rel (user_id,role_id,username)    VALUES(userId,roleId,userName)

### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'userId' in 'field list'

 

<insert id="addUserRoleRelation" parameterType="com.enterprise.domain.MerchantUserRole">

INSERT INTO b_merchat_user_role_rel(user_id,role_id,username)

VALUES(userId,roleId,userName)

</insert>

 

改成下面的形式问题解决

<insert id="addUserRoleRelation" parameterType="com.enterprise.domain.MerchantUserRole">

INSERT INTO b_merchat_user_role_rel(user_id,role_id,username)

VALUES(#{userId},#{roleId},#{userName})

</insert>

 

你可能感兴趣的:(Mybatis Mysql)