MyBatis DAO層開發——Mapper動態代理方式,mybatismapper
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//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 mapperPUBLIC "-//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; @Before public void init() throws Exception{ SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml"); sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream); } @Test public void testGetUserById() { SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = userMapper.getUserById(10); sqlSession.close(); System.out.println(user); } @Test public 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); } } @Test public 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; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", sex=" + sex + ", birthday=" + birthday + ", address=" + address + "]"; }}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。 http://blog.csdn.net/a772304419/article/details/79069308