zoukankan      html  css  js  c++  java
  • MyBatis

    1.首先要准备:


     2.实体类与小配置

    //实体类
    public class Dept {
    	private Integer deptNo;
    	private String deptName;
    	public Integer getDeptNo() {
    		return deptNo;
    	}
    	public void setDeptNo(Integer deptNo) {
    		this.deptNo = deptNo;
    	}
    	public String getDeptName() {
    		return deptName;
    	}
    	public void setDeptName(String deptName) {
    		this.deptName = deptName;
    	}
    	
    }
    //小配置 XXX.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">
    <mapper namespace="my.entity.Dept">
    <!-- 查询部门所有信息 -->
    <select id="selectAllDept" resultType="Dept">
    select * from MyDept
    </select>
    <!--条件查询 -->
    <select id="selectAllDeptById" parameterType="Dept" resultType="Dept">
    select * from MyDept where deptNo=#{deptNo}
    </select>
    <!-- 删除 -->
    <delete id="deleteAllDeptById" parameterType="Dept" >
    delete from dept where deptNo=#{deptNo}
    </delete>
    <!-- 修改 -->
    <update id="updateAllDeptById" parameterType="Dept" >
    update dept set deptName=#{deptName} where deptNo=#{deptNo}
    </update>
    <!-- 增加 -->
    <insert id="insertAllDeptById" parameterType="Dept" >
    insert into dept values(#{deptNo},#{deptName},#{deptCity})
    </insert>
    </mapper>

      


     3.大配置(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>
    <!--  别名的配置      Dept-->
    	<typeAliases>
    		<typeAlias type="my.entity.Dept" alias="Dept"/>
    	</typeAliases>
    	
    	<environments default="development">
    		<environment id="development">
    			<transactionManager type="JDBC" />
    			<dataSource type="POOLED">
    			    <property name="driver" value="oracle.jdbc.OracleDriver" />
    				<property name="url" value="jdbc:oracle:thin:@localhost:1521:LIYANG" />
    				<property name="username" value="LiYang" />
    				<property name="password" value="orcl" />
    			</dataSource>
    		</environment>
    	</environments>
    	
    	<mappers>
    		<mapper resource="my/entity/Dept.xml" />
    	</mappers>
    	
    
    </configuration>
    

     


    4. 测试类

    SqlSession session;
    @Before
    public void initData() throws IOException{
    Reader reader=Resources.getResourceAsReader("mybatis-config.xml");
    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
    session= factory.openSession();
    }
    @Test
    public void testselectAllDept() throws IOException{
    //在xml配置中的一个锁定唯一SQL的id
    List<Dept> selectList = session.selectList("selectAllDept");
    for (Dept dept : selectList) {
    System.out.println(dept);
    }
    }
    //模糊查詢
    @Test
    public void likeTest(){
    Dept dept = new Dept();
    dept.setDeptName("市场");
    List<Dept> list = session.selectList("mybatis.entity.Dept.likeDept",dept);
    for (Dept item : list) {
    System.out.println(item);
    }
    session.close();
    }
    //修改
    @Test
    public void updateTest(){
    Dept dept = new Dept();
    dept.setDeptNo(5);
    dept.setDeptName("开发部");
    int count = session.update("mybatis.entity.Dept.updateDept",dept);
    session.commit();
    System.out.println(count+"修改成功");
    session.close();
    }
    //删除
    @Test
    public void testdeleteDept() throws IOException{
    Dept dept = new Dept();
    dept.setDeptNo(5);
    int count = session.delete("mybatis.entity.Dept.deleteDept",dept);
    session.commit();
    System.out.println(count+"删除成功!");
    }
    //增加
    @Test
    public void testinsertDept() throws IOException{
    Dept dept = new Dept();
    dept.setDeptNo(8);
    dept.setDeptName("财务部");
    dept.setDeptCity("上海");
    int count = session.insert("mybatis.entity.Dept.insertDept",dept);
    session.commit();
    System.out.println(count+"增加成功");
    }
    }


      

  • 相关阅读:
    Github上优秀的.NET Core开源项目的集合
    阿里云服务器CentOS7.5 部署RabbitMQ
    Centos7开放及查看端口
    虚拟机CentOS7.0 部署Redis 5.0.8 集群
    CentOS7 服务检查命令
    CentOS7 常用命令集合
    pyflink小试牛刀
    python mysql pymysql where 当多条件查询,动态出现某些查询条件为空,就不作为条件查询的情况
    centOS6.2 最小安装下的无线网络配置
    Linux命令提示符的配置
  • 原文地址:https://www.cnblogs.com/ainiaiwo/p/5881858.html
Copyright © 2011-2022 走看看