1.User类
package com.domain; import java.io.Serializable; import java.util.Date; public class User implements Serializable{ private Integer id; private String username; private String address; private String sex; private Date birthday; 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 String getAddress() { return address; } public void setAddress(String address) { this.address = address; } 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; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", address='" + address + '\'' + ", sex='" + sex + '\'' + ", birthday=" + birthday + '}'; } }
2.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="mysql"> <environment id="mysql"> <!--配置事务--> <transactionManager type="JDBC"></transactionManager> <!--配置连接池--> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/eesy_mybatis"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </dataSource> </environment> </environments> <!--映射配置文件的位置--> <!--注意这里用/--> <mappers> <mapper resource="com/dao/IUserDao.xml"></mapper> </mappers> </configuration>
3.IUserDao接口
package com.dao; import com.domain.QueryVo; import com.domain.User; import java.util.List; //用户的持久层接口 public interface IUserDao { //查询所有 List<User> findall(); //根据id查询 User findbyid(Integer id); /** *保存用户 * @param user * @return 影响数据库记录的行数 */ int saveuser(User user); /** * 更新用户 * @param user * @return */ int updateUser(User user); /** * 根据id删除用户 * @param id * @return */ int deleteUser(Integer id); /** * 根据名称模糊查询 * @param username * @return */ List<User> finguserbyname(String username); /** * 查询总记录数 * @return */ int findtotal(); /** * 根据QueryVo中的条件查询用户 * @param vo * @return */ List<User> findbyvo(QueryVo vo); }
4.IUserDao.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"> <!--namespace确定该方法在哪个dao接口中--> <mapper namespace="com.dao.IUserDao"> <!--id来确定是具体哪个方法--> <!--id+namespace确定了一个固定的方法--> <!--resulttype表示要封装到那里去--> <!--里面写的是sql语句--> <select id="findall" resultType="com.domain.User"> SELECT * FROM user; </select> <!--select * from user where id = ? ;--> <!----> <select id="findbyid" parameterType="INT" resultType="com.domain.User"> select * from user where id = #{id}; </select> <!--配置保存是获取插入的id--> <insert id="saveuser" parameterType="com.domain.User"> <selectKey keyColumn="id" keyProperty="id" resultType="int"> select last_insert_id(); </selectKey> insert into user (username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address}); </insert> <update id="updateUser" parameterType="com.domain.User"> update user set username=#{username},birthday=#{birthday},sex=#{sex}, address=#{address} where id=#{id} </update> <delete id="deleteUser" parameterType="java.lang.Integer"> delete from user where id = #{id}; </delete> <!--select * from user where username like #{username};--> <select id="finguserbyname" resultType="com.domain.User" parameterType="String"> select * from user where username like '%${value}%'; </select> <select id="findtotal" resultType="int"> select count(*) from user; </select> <select id="findbyvo" resultType="com.domain.User" parameterType="com.domain.QueryVo"> select * from user where username like #{user.username}; </select> </mapper>
5测试类MybatisTest
package com.test; import com.dao.IUserDao; import com.domain.QueryVo; import com.domain.User; 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.After; import org.junit.Before; import org.junit.Test; import java.io.InputStream; import java.util.Date; import java.util.List; //测试mybatis的crud方法 public class MybatisTest { private InputStream in ; private SqlSessionFactory factory; private SqlSession session; private IUserDao userDao; // 测试查询所有方法 @Before //在测试之前执行 public void init()throws Exception { //1.读取配置文件 in = Resources.getResourceAsStream("SqlMapConfig.xml"); //2.创建构建者对象 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); //3.创建 SqlSession 工厂对象 factory = builder.build(in); //4.创建 SqlSession 对象 session = factory.openSession(); //5.创建 Dao 的代理对象 userDao = session.getMapper(IUserDao.class); } //根据id来查询 @Test public void testFindOne() { //6.执行操作 User user = userDao.findbyid(41); System.out.println(user); } @Test public void saveuser(){ User user = new User(); user.setUsername("User property"); user.setAddress("北京市顺义区"); user.setSex("男"); user.setBirthday(new Date()); System.out.println("保存操作之前: "+user); //5.执行保存方法 userDao.saveuser(user); System.out.println("保存操作之后: "+user); } @Test public void updateuser(){ User user1 = userDao.findbyid(41); //更新操作 user1.setAddress("河南"); //更新用户 int i = userDao.updateUser(user1); System.out.println(user1); System.out.println(i); } @Test public void deleteuser(){ //根据id删除用户 // User user = userDao.findbyid(50); userDao.deleteUser(50); } @Test public void finduserbyname(){ List<User> list = userDao.finguserbyname("王"); for (int i = 0; i < list.size(); i++) { User user = list.get(i); System.out.println(user); } } @Test public void findcount(){ int findtotal = userDao.findtotal(); System.out.println(findtotal); } //测试包装类作为参数,这个肯定远远不止这个简单的 @Test public void testfindbyQueryVo(){ //创建包装类 QueryVo vo = new QueryVo(); User user = new User(); //封装对象 user.setUsername("%王%"); vo.setUser(user); List<User> list = userDao.findbyvo(vo); for (int i = 0; i < list.size(); i++) { User user1 = list.get(i); System.out.println(user1); } } @After//在测试方法执行完成之后执行 public void destroy() throws Exception{ //提交事务 session.commit(); //7.释放资源 session.close(); in.close(); } }
6.加一个包装类的情况QueryVo.java
package com.domain; import java.io.Serializable; public class QueryVo implements Serializable { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
总结:主要是IUserDao接口 IUserDao.xml和MybatisTest测试类之间的代码其他的变动不大