zoukankan      html  css  js  c++  java
  • 2.MyBatis的crud

    以下例子纯属个人新手学习练手的,各位大神看了,莫吐槽。有不对的地方请多多指教。

    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();
    				}
    			}
    		}
    	}
    }
    

      

      

  • 相关阅读:
    MySQL行(记录)的详细操作一 介绍 二 插入数据INSERT 三 更新数据UPDATE 四 删除数据DELETE 五 查询数据SELECT 六 权限管理
    {MySQL完整性约束}一 介绍 二 not null与default 三 unique 四 primary key 五 auto_increment 六 foreign key 七 作业
    { MySQL基础数据类型}一 介绍 二 数值类型 三 日期类型 四 字符串类型 五 枚举类型与集合类型
    MySQL的sql_mode模式说明及设置
    {MySQL存储引擎介绍}一 存储引擎解释 二 MySQL存储引擎分类 三 不同存储引擎的使用
    {MySQL的库、表的详细操作}一 库操作 二 表操作 三 行操作
    小爬爬2.数据解析
    小爬爬2:fiddler安装和了解fiddler
    小爬爬2.回顾
    小爬爬1.requests基础操作
  • 原文地址:https://www.cnblogs.com/tournesol/p/8094598.html
Copyright © 2011-2022 走看看