TbUserMapper.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="org.liyb.mybatis.dao.TbUserMapper" >
<resultMap id="BaseResultMap" type="org.liyb.mybatis.model.TbUser" >
<id column="uname" property="uname" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="phone" property="phone" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
</resultMap>
<!-- 20160523 增添用户-->
<insert id="insertUser" parameterType="org.liyb.mybatis.model.TbUser" >
insert into tb_user (uname, name, phone, password)
values
(#{uname,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR})
</insert>
<select id="selectListByName" parameterType="String" resultType="TbUser">
SELECT
tu.uname,
tu.name,
tu.phone,
tu.password
FROM tb_user tu
where tu.name=#{name,jdbcType=VARCHAR}
</select>
<update id="updateByName" parameterType="TbUser">
update tb_user tu
set tu.uname = #{uname},
tu.name = #{name},
tu.phone = #{phone},
tu.password= #{password}
WHERE tu.uname = 'songzh1';
</update>
<delete id="deleteByName" parameterType="String">
DELETE FROM tb_user tu
WHERE tu.uname = 'songzh2';
</delete>
</mapper>
TbUserMapper.java
package org.liyb.mybatis.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.liyb.mybatis.model.TbUser;
import org.liyb.mybatis.model.TbUserExample;
/**
* 基于接口的mybatis变成方式
*/
public interface TbUserMapper {
int updateByPrimaryKeySelective(TbUser record);
int updateByName(TbUser record);
void insertUser(TbUser user);
TbUser getUser(String name);
int deleteByName(String record);
}
Test
package test.mybatis;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.liyb.mybatis.dao.TbUserMapper;
import org.liyb.mybatis.model.TbUser;
/**
* @author lee
* Description:
* @date 2016年5月24日
*/
public class TestMyBatis {
public static SqlSessionFactory sqlSessionFactory = null;
static{
sqlSessionFactory = MyBatisUtil.getSqlsessionfactory();
}
/**
* description:mybatis插入操作
* @param
* @return void
*
*/
public static void testAdd() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
TbUserMapper userMapper = sqlSession.getMapper(TbUserMapper.class);
TbUser user = new TbUser("songzh1", "songzhihui","1860138753","123");
userMapper.insertUser(user);//与SqlMap中添加用户信息方法对应
sqlSession.commit();// 这里一定要提交,不然数据进不去数据库中
} finally {
sqlSession.close();
}
}
/**
* description:mybatis查询操作
* @param name
* @return void
*
*/
public static void selectByName(String name) {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
TbUserMapper userMapper = sqlSession.getMapper(TbUserMapper.class);
TbUser tu = userMapper.selectListByName(name);
System.out.println(tu.getName());
} finally {
sqlSession.close();
}
}
/**
* description:mybatis更新操作
* @param TbUser
* @return void
*
*/
public static void updateByName(TbUser user){
SqlSession sqlSession = sqlSessionFactory.openSession();
TbUserMapper userMapper = sqlSession.getMapper(TbUserMapper.class);
int count = userMapper.updateByName(user);
sqlSession.commit();
System.out.println(count);
}
/**
* description:mybatis删除操作
* @param user
* @return void
*
*/
public static void deleteByName(String name){
SqlSession sqlSession = sqlSessionFactory.openSession();
TbUserMapper userMapper = sqlSession.getMapper(TbUserMapper.class);
int count = userMapper.deleteByName(name);
sqlSession.commit();
System.out.println(count);
}
public static void main(String[] args){
// TestMyBatis.testAdd();
// TestMyBatis.selectByName("qianjunyi");
// TbUser user = new TbUser("songzh2", "songzhihui2","1860138753","123");
// TestMyBatis.updateByName(user);
TestMyBatis.deleteByName("songzh2");
}
}