Mybatis编写dao实现类的CRUD
1.pom.xml
<dependencies> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.11version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>5.1.41version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatisartifactId> <version>3.4.6version> dependency> dependencies>
2.SqlMapperConfig.xml
xml version="1.0" encoding="UTF-8" ?> DOCTYPEconfiguration 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/zml?useUnicode=true&characterEncoding=utf-8">property> <property name="username" value="root">property> <property name="password" value="root">property> dataSource> environment> environments> <mappers> <mapper resource="dao/UserDao.xml">mapper> mappers> configuration>
3.Users.java
package entity; public class Users { private int id; private String username; private String password; 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 getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "Users{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; } }
4.UserDao.java
package dao; import entity.Users; import java.util.List; public interface UserDao { ListfindAll(); Users find(int id); void save(Users users); void update(Users users); void delete(int id); List findVague(String name); int count(); }
5.UserDao.xml
xml version="1.0" encoding="UTF-8" ?> DOCTYPEmapper PUBLIC"-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="dao.UserDao"> <select id="findAll" resultType="entity.Users"> select * from users select> <select id="find" resultType="entity.Users"> select * from users where id=#{id} select> <insert id="save" parameterType="entity.Users"> <selectKey keyProperty="id" keyColumn="id" resultType="int" order="AFTER"> select last_insert_id() selectKey> insert into users(username,password) values(#{username},#{password}) insert> <update id="update" parameterType="entity.Users"> update users set username=#{username},password=#{password} where id=#{id} update> <delete id="delete" parameterType="entity.Users"> delete from users where id=#{id} delete> <select id="findVague" resultType="entity.Users"> -- select * from users where username like '%${value}%' select * from users where username like #{name} select> <select id="count" resultType="int"> select count(id) from users select> <select id="findVo" resultType="entity.Users"> select * from users where username like #{users.username} select> mapper>
6.UserDaoImpl.java
package dao.impl; import dao.UserDao; import entity.Users; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import java.util.List; public class UserDaoImpl implements UserDao { private SqlSessionFactory factory; public UserDaoImpl(SqlSessionFactory factory){ this.factory=factory; } @Override public ListfindAll() { //1.根据factory获取SqlSession对象 SqlSession session=factory.openSession(); //2.调用SqlSession中的方法,实现查询列表 List list=session.selectList("dao.UserDao.findAll"); //3.释放资源 session.close(); return list; } @Override public Users find(int id) { //1.根据factory获取SqlSession对象 SqlSession session=factory.openSession(); //2.调用SqlSession中的方法,实现查询列表 Users u=session.selectOne("dao.UserDao.find",id); //3.释放资源 session.close(); return u; } @Override public void save(Users users) { //1.根据factory获取SqlSession对象 SqlSession session=factory.openSession(); //2.调用SqlSession中的方法,实现查询列表 session.insert("dao.UserDao.save",users); session.commit(); //3.释放资源 session.close(); } @Override public void update(Users users) { //1.根据factory获取SqlSession对象 SqlSession session=factory.openSession(); //2.调用SqlSession中的方法,实现查询列表 session.update("dao.UserDao.update",users); session.commit(); //3.释放资源 session.close(); } @Override public void delete(int id) { //1.根据factory获取SqlSession对象 SqlSession session=factory.openSession(); //2.调用SqlSession中的方法,实现查询列表 session.delete("dao.UserDao.delete",id); session.commit(); //3.释放资源 session.close(); } @Override public List findVague(String name) { //1.根据factory获取SqlSession对象 SqlSession session=factory.openSession(); //2.调用SqlSession中的方法,实现查询列表 List u=session.selectList("dao.UserDao.findVague",name); session.commit(); //3.释放资源 session.close(); return u; } @Override public int count() { //1.根据factory获取SqlSession对象 SqlSession session=factory.openSession(); //2.调用SqlSession中的方法,实现查询列表 int id=session.selectOne("dao.UserDao.count"); session.commit(); //3.释放资源 session.close(); return id; } }
7.UserTest.java
package test; import dao.UserDao; import dao.impl.UserDaoImpl; import entity.Users; 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.IOException; import java.io.InputStream; import java.util.List; public class UserTest { private InputStream in; private UserDao userDao; @Before public void init() throws IOException { //1.读取配置文件 in= Resources.getResourceAsStream("SqlMapperConfig.xml"); //2.创建SqlSessionFactory工厂 SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder(); SqlSessionFactory factory=builder.build(in); //3.使用工厂对象,创建dao对象 userDao=new UserDaoImpl(factory); } @After public void destroy() throws IOException { //6.释放资源 in.close(); } /** * 查询所有 * @throws IOException */ @Test public void findall() throws IOException { //5.使用代理对象执行方法 Listusers=userDao.findAll(); for(Users u:users){ System.out.println(u); } } /** * 增加 */ @Test public void save(){ Users users=new Users(); users.setUsername("111"); users.setPassword("456"); userDao.save(users); } /** * 根据ID查询并修改 */ @Test public void update(){ Users u=userDao.find(15); u.setPassword("126"); u.setUsername("小喵"); userDao.update(u); } /** * 根据id进行删除 */ @Test public void delete(){ userDao.delete(24); } /** * 模糊查询,查询所有名字中有小的用户 * @throws IOException */ @Test public void findVague() throws IOException { //5.使用代理对象执行方法 List users=userDao.findVague("%小%"); for(Users u:users){ System.out.println(u); } } /** * 查询用户总数 */ @Test public void count(){ int c=userDao.count(); System.out.println(c); } }