后台-插件-广告管理-内容页广告位一(手机)

您现在的位置是:首页 > 开发类 > 软件研发软件研发

Mybaits入门

2021-06-07 21:02:39软件研发人已围观

简介mybaits入门

Mybaits入门

一、先配置主配置文件xml(mybaits-config.xml) 导入mybatis包(这里会链接MySQL数据库所以MySQL包也得导入) 注意:这里创建的实例是java项目
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!-- 约束 -->
 3 <!DOCTYPE configuration
 4   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 5   "http://mybatis.org/dtd/mybatis-3-config.dtd">
 6 
 7   <configuration>
 8       <!-- 导入properties 文件 -->
 9       <properties resource="db.properties"></properties>
10       
11         <!--环境配置(链接数据库) -->
12       <environments default="development">
13            <environment id="development">
14                <!-- 事务控制 -->
15                <transactionManager type="JDBC" />
16                <!-- 数据源 -->
17                <dataSource type="POOLED">
18                    <property name="driver" value="${driver}"/>
19                    <property name="url" value="${url}"/>
20                    <property name="username" value="${username}"/>
21                    <property name="password" value="${password}"/>
22                </dataSource>
23            </environment>
24       </environments>
25       
26       <!-- 引入映射文件 -->
27       
28       <mappers>
29           <mapper resource="mybatis-student.xml"/>
30       </mappers>
31   </configuration>

二、建立properties联系数据源

1 driver=com.mysql.jdbc.Driver
2 url=jdbc:mysql:///bj
3 username=root
4 password=123456
三、配置映射文件(mybatis-student.xml)(增删改查在这里进行)
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3   PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4   "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5   
 6 <mapper namespace="student">
 7     <insert id="addstudent" parameterType="org.laogao.pojo.student">
 8         insert into student values(#{id},#{name},#{age},#{sex});
 9     </insert>
10     
11     <delete id="delete" parameterType="org.laogao.pojo.student">
12         delete from  student where id=#{id}
13     </delete>
14   
15   <update id="updatestudent" parameterType="org.laogao.pojo.student">
16       update student set name=#{name},age=#{age},sex=#{sex} where id=#{id}
17   
18   
19   </update>
20   
21   <select id="showstudent"  resultType="org.laogao.pojo.student">
22   
23       select * from student 
24   </select>
25   
26   <select id="showstudentone" resultType="org.laogao.pojo.student" parameterType="org.laogao.pojo.student">
27   
28   select * from student where id=#{id}
29   
30   </select>
31 </mapper>
四、建立测试类(这种公司里基本不用,但利于新手理解,了解即可) 常用类型请看mybaits进阶
public class TestMybatis  {
    @Test
 //写法用的是JUNIT
    public void addstudent() throws IOException{
        //解析主配置文件成一个输入流
        InputStream stream = Resources.getResourceAsStream("mybaits-config.xml");
        //sqlsession工厂
        //通过SqlSessionFactoryBuilder.build(stream)构建实例获得SqlSessionFactory
        SqlSessionFactory SqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        
        //生成sql
        SqlSession session = SqlSessionFactory.openSession();
        //写一条数据
        student student = new student(0, "hp", "12", "nv");
        //增加
        session.insert("student.addstudent", student);
        session.commit();
    }
 
 
    @Test
    public void deletestudentbyid() throws Exception{
        //解析主配置文件成一个输入流
        InputStream stream = Resources.getResourceAsStream("mybaits-config.xml");
        SqlSessionFactory SqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        
        SqlSession session = SqlSessionFactory.openSession();
        
        session.delete("student.delete", 22);
        session.commit();
    }
    @Test
    public void updatestudentnyid() throws Exception{
        InputStream stream = Resources.getResourceAsStream("mybaits-config.xml");
        
        SqlSessionFactory SqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        
        SqlSession session = SqlSessionFactory.openSession();
        
        student student = new student(15, "sb", "20", "s");
        session.update("student.updatestudent", student);
        session.commit();
    }
    
    @Test
    public void showstudent() throws Exception{
        InputStream stream = Resources.getResourceAsStream("mybaits-config.xml");
        
        SqlSessionFactory SqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        
        SqlSession session = SqlSessionFactory.openSession();
        List<student> studentlist=session.selectList("student.showstudent",null);
        for (student student : studentlist) {
            System.out.println(student.toString());
            
        }
        
    }
    
    
    
    @Test
    public void showstudentone() throws Exception{
        InputStream stream = Resources.getResourceAsStream("mybaits-config.xml");
        
        SqlSessionFactory  SqlSessionFactory= new SqlSessionFactoryBuilder().build(stream);
        
        SqlSession session = SqlSessionFactory.openSession();
        
        
        Object student = session.selectOne("student.showstudentone", 15);
        System.out.println(student.toString());
    }
}

文章来源:https://www.cnblogs.com/laogao2333/p/14842859.html

Tags:

很赞哦! ()

后台-插件-广告管理-内容页广告位二(手机)

相关文章

后台-插件-广告管理-内容页广告位三(手机)
后台-插件-广告管理-内容页广告位四(手机)

文章评论

留言与评论(共有 0 条评论)
   
验证码:

本栏推荐

站点信息

  • 文章统计60330篇文章
  • 浏览统计4607次浏览
  • 评论统计1个评论
  • 标签管理标签云
  • 统计数据:统计代码
  • 微信公众号:扫描二维码,关注我们