以下例子纯属个人新手学习练手的,各位大神看了,莫吐槽。有不对的地方请多多指教。
1.建一个项目:
2.导入jar
3.MySql数据库建库建表:
4.项目里面创建配置文件,包:
4.1mybatis-config.xml配置文件
4.2创建包 com.pxf.entity com.pxf.dao com.pxf.mapper com.pxf.test
5.mybatis-config.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 --> <environment id="development"> <!-- 事物管理器 --> <transactionManager type="JDBC" /> <!-- 连接池 POOLED是mybatis提交的连接池 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql:///1708_mybatis" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <!-- Mapper文件 --> <mappers> <mapper resource="com/pxf/mapper/IUserDao.xml" /> </mappers> </configuration>
6.在包com.pxf.entity创建实体类User:
package com.pxf.entity; public class User { private Integer id; private String name; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public User() { super(); } public User(Integer id, String name, String password) { super(); this.id = id; this.name = name; this.password = password; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", password=" + password + "]"; } }
7.在包com.pxf.dao创建接口IUserDao:
package com.pxf.dao; import java.util.List; import com.pxf.entity.User; public interface IUserDao { public User getUserById(Integer id); public void add(User user); public void update(User user); public void delete(Integer id); public List<User> getUserList(); }
8.在包com.pxf.mapper创建接口的映射文件:
<?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指接口的全类名 --> <mapper namespace="com.pxf.dao.IUserDao"> <!-- id:方法名称 resultType:返回类型 --> <select id="getUserById" resultType="com.pxf.entity.User"> SELECT * FROM t_user WHERE id = #{id} </select> <insert id="add"> INSERT INTO t_user (NAME, PASSWORD) VALUES (#{name},#{password} ) </insert> <update id="update"> UPDATE t_user SET NAME = #{name}, PASSWORD = #{password} WHERE id = #{id} </update> <!-- parameterType 方法返回集合的时候resultType要配置集合中的类型 --> <delete id="delete" parameterType="java.lang.Integer"> DELETE FROM t_user WHERE id = #{id} </delete> <select id="getUserList" resultType="com.pxf.entity.User"> SELECT * FROM t_user </select> </mapper>
9测试类:
package com.pxf.test; import java.io.IOException; import java.io.InputStream; import java.util.List; import java.util.ListIterator; 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.Test; import com.pxf.dao.IUserDao; import com.pxf.entity.User; public class UserDaoTest { @Test public void testGetUserById() { String resource = "mybatis-config.xml"; try { InputStream stream = Resources.getResourceAsStream(resource); SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(stream); SqlSession sqlSession = sqlSessionFactory.openSession(); IUserDao userDao = sqlSession.getMapper(IUserDao.class); User user = userDao.getUserById(2); System.out.println(user); } catch (IOException e) { e.printStackTrace(); } } @Test public void testAddUser() { String resource = "mybatis-config.xml"; InputStream stream = null; try { stream = Resources.getResourceAsStream(resource); SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(stream); SqlSession sqlSession = sqlSessionFactory.openSession(); IUserDao userDao = sqlSession.getMapper(IUserDao.class); User user = new User(); user.setName("范冰冰"); user.setPassword("123"); userDao.add(user); sqlSession.commit(); sqlSession.close(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (stream != null) { stream.close(); } } catch (IOException e) { e.printStackTrace(); } } } @Test public void testUpdateUser() { String resource = "mybatis-config.xml"; InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream(resource); SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); IUserDao userDao = sqlSession.getMapper(IUserDao.class); User user = userDao.getUserById(3); user.setName("莉莉"); user.setPassword("1234"); userDao.update(user); sqlSession.commit(); sqlSession.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } @Test public void testDeleteById() { String resource = "mybatis-config.xml"; InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream(resource); SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); IUserDao userDao = sqlSession.getMapper(IUserDao.class); userDao.delete(5); sqlSession.commit(); sqlSession.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } @Test public void testGetUserList() { String resource = "mybatis-config.xml"; InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream(resource); SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream); SqlSession openSession = sqlSessionFactory.openSession(); IUserDao userDao = openSession.getMapper(IUserDao.class); List<User> userList = userDao.getUserList(); for (User user : userList) { System.out.println(user); } openSession.commit(); openSession.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } }