zoukankan      html  css  js  c++  java
  • Mybaits 笔记1,接口编程,缺少resources目录

    1. 新建maven工程 ,id为quickstart,然后从properties里面的java build path,添加目录,resources目录。默认的eclipse不带resources目录。

    具体步骤如下,在src下,main下,建立resource目录,然后在buildpath里,sources选项卡,然后点击add folder,然后选中刚才建立的resources,即可。

    2. pom建立坐标,

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.6</version>
    </dependency>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
        
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.48</version>
    </dependency>

    3. 编写实体bean类,如Employee

    4.编写接口,如EmployeeMapper,这里的接口的名字需要和mapper的namespace的全类名相同,另外接口的方法,要和mapper的id相同。

    5.编写mybaits-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 id="development">
          <transactionManager type="JDBC"/>
          <dataSource type="POOLED">
            <property name="driver" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/test"/>
            <property name="username" value="root"/>
            <property name="password" value="123456"/>
          </dataSource>
        </environment>
      </environments>
      <mappers>
        <mapper resource="EmployeeMapper.xml"/>
      </mappers>
    </configuration>

    6.编写EmployeeMapper

    <?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="cn.taotao.dao.EmployeeMapper">   //此处的命名空间必须和接口的类名相同,下面的id必须和接口的方法名相同
      <select id="getEmployeeById" resultType="cn.taotao.bean.Employee">
        select * from tbl_employee where id = #{id}
      </select>
    </mapper>

    7.写测试类,注意一定要和bean和接口的名字有区分,比如EmployeeTest,如果相同,会导致导入包的错误。

    @Test
        public void test()  {
            String resource = "mybatis-config.xml";
            SqlSession ss = null;
            try {
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(inputStream);
            ss = ssf.openSession();
            EmployeeMapper mapper = ss.getMapper(EmployeeMapper.class);
            Employee employee  = mapper.getEmployeeById(1);
            System.out.println(employee.toString());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
            ss.close();    
            }
        }

    又如:

        @Test
        public void test() {
            String resource = "mybatis-config.xml";
            SqlSession sqlSession = null;                   //非线程安全的对象,不能作为成员变量,必须每次获取。在多线程下,A线程可能把他关了,B线程可能在用。为避免,必须每次获取。
            try {
                InputStream inputStream = Resources.getResourceAsStream(resource);
                SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
                sqlSession = sqlSessionFactory.openSession();
                EmployeeMapperDao empMapperDao = sqlSession.getMapper(EmployeeMapperDao.class);     //接口类型,接口类
                Employee emp = empMapperDao.getEmpById(1);                                           //接口里面的方法   
                System.out.println(emp.toString());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                sqlSession.close();
            }
            
        }

    另外的两种方式创建数据库

    在/src/main/resource 下建立 activiti.cfg.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    
        <bean id="processEngineConfiguration"
            class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
            <property name="jdbcDriver" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl"
                value="jdbc:mysql://localhost:3306/activitidb"></property>
            <property name="jdbcUsername" value="root"></property>
            <property name="jdbcPassword" value="123456"></property>
            <property name="databaseSchemaUpdate" value="true"></property>
        </bean>
    </beans>

    代码(2个示例)

    @Test
        public void createDb2() {
            ProcessEngineConfiguration config = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
             config.buildProcessEngine();
        }
        
        @Test
        public void createDB3() {
            ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();   //这里是复数形式 ,带s
        }
  • 相关阅读:
    C# 实现 Snowflake算法生成唯一性Id
    kafka可视化客户端工具(Kafka Tool)的基本使用(转)
    docker 安装kafka
    Model类代码生成器
    使用docker 部署rabbitmq 镜像
    Vue 增删改查 demo
    git 提交代码到库
    Android ble蓝牙问题
    mac 配置 ssh 到git (Could not resolve hostname github.com, Failed to connect to github.com port 443 Operation timed out)
    okhttp
  • 原文地址:https://www.cnblogs.com/sdgtxuyong/p/11596503.html
Copyright © 2011-2022 走看看