1. 程式人生 > 實用技巧 >OpenCV 感興趣區域ROI和logo新增技術

OpenCV 感興趣區域ROI和logo新增技術

技術標籤:框架mybatis

Mybatis

依賴

 <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <dependency
>
<groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</
artifactId
>
<version>4.12</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency
>
</dependencies>

#mybatis配置檔案

<?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>
    <properties resource="db.properties"/>
    <!--配置日誌檔案log4j-->
    <!--    <settings>
            <setting name="logImpl" value="LOG4J"/>
            </settings>-->
    <!--可以給實體類起別名,還可以通過掃掃描包來 -->
    <typeAliases>
        <package name="com.kuang.pojo"/>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper class="com.kuang.dao.TeacherMapper"/>
        <mapper class="com.kuang.dao.StudentMapper"/>
    </mappers>

</configuration>

##其他配置

<!--配置日誌檔案log4j-->
<!--    <settings>
        <setting name="logImpl" value="LOG4J"/>
    </settings>-->
    <!--可以給實體類起別名,還可以通過掃掃描包來 -->
    <!--是否開啟駝峰命名自動對映,即從經典資料庫列名 A_COLUMN 對映到經典 Java 屬性名 aColumn。-->
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <typeAliases>
        <package name="com.kuang.pojo"/>
    </typeAliases>

##工具類

public class MybatisUtils {
    private  static SqlSessionFactory sqlSessionFactory;
   static {

       try {
           String resource = "mybatis-config.xml";
           InputStream inputStream = Resources.getResourceAsStream(resource);
           sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
       } catch (IOException e) {
           e.printStackTrace();
       }
   }
   public static SqlSession getSession(){
       return sqlSessionFactory.openSession();

   }
}

##一對多和多對一的interface
左邊是一對多,右邊多對一

##一對多Mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--核心配置檔案-->
<mapper namespace="com.kuang.dao.TeacherMapper">
    <!--按照結果巢狀查詢-->
    <select id="getTeacher2" resultMap="TeacherStudent">
        select s.id sid,s.name sname,t.id tid,t.name tname
        from student s,teacher t
        where s.tid=t.id and t.id=#{tid}
    </select>
    <resultMap id="TeacherStudent" type="Teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
        <collection property="students" ofType="Student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>
    <!--複雜屬性的對映,我們需要單獨處理  物件:association javaType=""指定屬性的型別
    集合collection 使用ofType=""獲取集合中的泛型資訊-->

    <!--=====================================================-->

    <!--按照查詢巢狀查詢-->
    <select id="getTeacher3" resultMap="TeacherStudent2">
        select * from mabatis.teacher where id=#{tid}
    </select>
    <resultMap id="TeacherStudent2" type="Teacher">
        <collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId" column="id"/>
    </resultMap>
    <select id="getStudentByTeacherId" resultType="Student">
        select * from mabatis.student where tid=#{tid}
    </select>
</mapper>

##多對一Mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--核心配置檔案-->
<mapper namespace="com.kuang.dao.StudentMapper">

    <!--按照結果巢狀處理-->
    <select id="getStudent2" resultMap="StudentTeacher2">
        select s.id sid,s.name sname,t.id tid,t.name tname
        from student s,teacher t
        where s.tid=t.id
    </select>
    <resultMap id="StudentTeacher2" type="Student">
        <association property="id" column="sid"/>
        <association property="name" column="sname"/>
        <association property="teacher" javaType="Teacher">
            <result property="name" column="tname"/>
            <result property="id" column="tid"/>
        </association>
    </resultMap>


    <!--=======================================================================-->
    <!--按照查詢巢狀處理-->
    <select id="getStudent" resultMap="StudentTeacher">
        select * from student
    </select>

    <resultMap id="StudentTeacher" type="Student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
    </resultMap>

    <select id="getTeacher" resultType="Teacher">
        select * from teacher where id=#{id}
    </select>
</mapper>

##動態sql

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kuang.dao.BlogMapper">
    <insert id="addBook" parameterType="blog">
        insert into mabatis.blog(id, title, author, create_time, views)
        values (#{id}, #{title}, #{author}, #{createTime}, #{views})
    </insert>

    <select id="queryBlogIf" parameterType="map" resultType="Blog">
        select * from mabatis.blog
        <where>
        <if test="title != null">
            and title=#{title}
        </if>
        <if test="author != null">
            and author=#{author}
        </if>
        </where>
    </select>

    <update id="updateBlog" parameterType="map">
        update  mabatis.blog
        <set>
            <if test="title != null">
                title=#{title},
            </if>
            <if test="author != null">
                author=#{author},
            </if>
        </set>
        where id=#{id}
    </update>

    <select id="queryBlogForeach" parameterType="map" resultType="Blog">
        select * from mabatis.blog
        <where>
            <foreach collection="ids" item="id" open="and (" close=")" separator="or">
                id = #{id}
            </foreach>
        </where>
    </select>
</mapper>

##測試類

  @Test
        public void queryBlogIf(){
            SqlSession session = MybatisUtils.getSession();
            BlogMapper mapper = session.getMapper(BlogMapper.class);
            HashMap map = new HashMap();
            //map.put("author","狂神說4444");
            map.put("title","java");
            //map.put("id","46b1c7ec3ffa47eb90638d53f241baa6");
           // map.put("author","狂神說");
            //mapper.updateBlog(map);
            List<Blog> blogs = mapper.queryBlogIf(map);
            for (Blog blog : blogs) {
                System.out.println(blog);
            }
            session.close();

    }