mybatis入门之简单操作

本文介绍了一种使用MyBatis框架通过动态代理的方式实现数据库操作的方法。文章详细展示了User实体类、UserDao接口及相应的XML映射文件,并提供了一个简单的测试类用于验证功能。通过这种方式,可以更方便地进行CRUD操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mapper动态代理方式 

bean
public class User {
	private Integer id;
	private String username;
	private Date birthday;
	private String sex;
	private String address;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}


	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public User() {
	}


	public User(Integer id, String username, Date birthday, String sex,
			String address) {
		super();
		this.id = id;
		this.username = username;
		this.birthday = birthday;
		this.sex = sex;
		this.address = address;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", birthday="
				+ birthday + ", sex=" + sex + ", address=" + address + "]";
	}
UserDao
public interface UserDao {
   /* 此处定义的方法名称必须和mapper文件里面各个id名称保持一致**/
   public User findUserById(int id);
   public List<User>findUserByUsername(String username);
   public void insertUser(User user);
   public void updateUser(User user);
   public void deleteUser(int id);
}
UserMapper.xml

注意:mapper配置文件里面的 namespace必须指向接口(包+类名)
<?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.zrf.mybatis.dao.UserDao">
   <select id="findUserById" parameterType="int" resultType="com.zrf.mybatis.bean.User">
     select * from user where id=#{id}
   </select>
   <select id="findUserByUsername" parameterType="java.lang.String" resultType="com.zrf.mybatis.bean.User">
      select * from user where username like '%${value}%'
   </select>
   <insert id="insertUser" parameterType="com.zrf.mybatis.bean.User">
     insert into user(username,birthday,sex,address)value(#{username},#{birthday},#{sex},#{address})
   </insert>
   <update id="updateUser" parameterType="com.zrf.mybatis.bean.User">
     update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}
   </update>
   <delete id="deleteUser" parameterType="int">
      delete from user where id=#{id}
   </delete>
</mapper>
SqlMapConfig.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>
   <environments default="developement">
      <environment id="developement">
        <transactionManager type="JDBC"/>
        <dataSource type="POOLED">
          <property name="driver" value="com.mysql.jdbc.Driver"/>
          <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8"/>
          <property name="username" value="root"/>
          <property name="password" value="000000"/>
        </dataSource>
      </environment>
   </environments>
   <mappers>
     <mapper resource="com/zrf/mybatis/mapping/UserMapper.xml"/>
   </mappers>
</configuration>
测试类
public class Test {
	public static void main(String[] args) throws Exception {
		String resource="com/zrf/mybatis/config/SqlMapConfig.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
		 SqlSession sqlSession =sqlSessionFactory.openSession();
		 UserDao userDao=sqlSession.getMapper(UserDao.class);
//		 User user=userDao.findUserById(29);
//		 List<User>userList=userDao.findUserByUsername("小明");
//		 User user=new User();
//		 user.setUsername("孙志平2");
//		 user.setBirthday(new Date());
//		 user.setSex("2");
//		 user.setAddress("山西太原2");
//		 user.setId(32);
//		 userDao.updateUser(user);
		 userDao.deleteUser(32);
		 sqlSession.commit();
		 sqlSession.close();
//		 System.out.println(userList);
	}
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值