欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

MyBatis DAO层开发——Mapper动态代理方式

发布时间:2025/1/21 编程问答 30 豆豆
生活随笔 收集整理的这篇文章主要介绍了 MyBatis DAO层开发——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="development"><environment id="development"><!-- 使用jdbc事务管理 --><transactionManager type="JDBC"/><!-- 数据库连接池 --><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8"/><property name="username" value="root"/><property name="password" value="011220"/></dataSource></environment></environments><mappers><mapper resource="sqlmap/user.xml"/><mapper resource="mapper/mapper.xml"/></mappers> </configuration>

mapper.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="cn.nwtxxb.mybatis.mapper.UserMapper"><!-- 根据用户id查询用户信息 --><select id="getUserById" parameterType="int" resultType="cn.nwtxxb.mybatis.po.User">select * from user where id = #{id}</select><!-- 根据用户名查询用户信息 --><select id="getUserByName" parameterType="java.lang.String" resultType="cn.nwtxxb.mybatis.po.User">select * from user where username like '%${value}%'</select><!-- 添加用户 --><insert id="insertUser" parameterType="cn.nwtxxb.mybatis.po.User"><!-- selectKey将主键返回 --><selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">select LAST_INSERT_ID()</selectKey>insert into user (username,birthday,sex,address) values (#{username},#{birthday},#{sex},#{address})</insert><!-- 删除用户 --><delete id="deleteUserById" parameterType="int">delete from user where id=#{id}</delete><!-- 修改用户 --><update id="updateUser" parameterType="int">update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}</update> </mapper>

UserMapper.java

package cn.nwtxxb.mybatis.mapper;import java.util.List;import cn.nwtxxb.mybatis.po.User;public interface UserMapper {User getUserById(int id);List<User> getUserByName(String name);void insertUser(User user); }

UserMapperTest.java

package cn.nwtxxb.mybatis.mapper.test;import java.io.InputStream; import java.util.Date; import java.util.List;import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Before; import org.junit.Test;import cn.nwtxxb.mybatis.mapper.UserMapper; import cn.nwtxxb.mybatis.po.User;public class UserMapperTest {private SqlSessionFactory sqlSessionFactory = null;@Beforepublic void init() throws Exception{SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);}@Testpublic void testGetUserById() {SqlSession sqlSession = sqlSessionFactory.openSession();UserMapper userMapper = sqlSession.getMapper(UserMapper.class);User user = userMapper.getUserById(10);sqlSession.close();System.out.println(user);}@Testpublic void testGetUserByName() {SqlSession sqlSession = sqlSessionFactory.openSession();UserMapper userMapper = sqlSession.getMapper(UserMapper.class);List<User> list = userMapper.getUserByName("张");sqlSession.close();for (User user : list) {System.out.println(user);}}@Testpublic void testInsertUser() {SqlSession sqlSession = sqlSessionFactory.openSession();UserMapper userMapper = sqlSession.getMapper(UserMapper.class);User user = new User();user.setUsername("汪明小盆友");user.setSex("2");user.setBirthday(new Date());user.setAddress("北极圈");userMapper.insertUser(user);sqlSession.commit();sqlSession.close();}}

User.java

package cn.nwtxxb.mybatis.po;import java.util.Date;public class User {private int id;private String username;private String sex;private Date birthday;private String address;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "User [id=" + id + ", username=" + username + ", sex=" + sex + ", birthday=" + birthday + ", address="+ address + "]";}}

总结

以上是生活随笔为你收集整理的MyBatis DAO层开发——Mapper动态代理方式的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。