码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis入门实例

时间:2017-09-14 18:48:11      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:resource   string   typealias   建立   parameter   code   ade   sre   --   

新建maven项目 加入mybatis依赖

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.5</version>
</dependency>

pom.xml配置如下

<?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.fz</groupId>
        <artifactId>mybatis03</artifactId>
        <version>1.0</version>
        <packaging>jar</packaging>
        <properties>
        <java-version>1.8</java-version>
        </properties>
        <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.43</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        </dependencies>
        <build>
        <finalName>${project.artifactId}</finalName>
        <testSourceDirectory>src/test/java</testSourceDirectory>
        <sourceDirectory>src/main/java</sourceDirectory>
        <!-- 处理无法加载资源配置文件 -->
        <resources>
            <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
            </resource>
            <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.xml</include>
                <include>**/*.properties</include>
            </includes>
            </resource>
        </resources>
        </build>
    </project>     

新建mybatis配置文件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>
    <typeAliases>
        <!-- 实体类别名 -->
        <typeAlias alias="member" type="com.ssz.entity.Member"/>
        <!-- 将实体包下的类自动为别名 com.fz.entity.User  别名就是 user-->
        <package name="com.ssz.entity"/>
    </typeAliases>
    <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?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=true"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/ssz/mapper/MemberMapper.xml"/>
    </mappers>
</configuration>

准备数据库 给个图 语句我就不写了。。。技术分享

编写实体类 com/ssz/entity/Member.java

package com.ssz.entity;

import lombok.Data;

@Data
public class Member {
    private int id;
    private String account;
    private String password;
    private String name;
}

建立映射文件  com/ssz/mapper/MemberMapper.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">

<!-- com/ssz/mapper/MemberMapper 影射配置文件位置 -->
<mapper namespace="com.ssz.mapper.MemberMapper">

    <!-- 此处结果类型 resultType 指定的member 为com.ssz.entity.Member实体类别名 -->
    <select id="query" resultType="member">
        select * from db_member
    </select>

    <delete id="deleteById" parameterType="int">
        delete from db_member where id=#{id}
    </delete>
</mapper>

编写测试类Demo.java

package com.ssz.entity;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSourceFactory;
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 java.io.IOException;
import java.util.List;

public class Demo {
    public static void main(String[] args) throws IOException {
        //建立数据库会话工厂
        SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
        //读取会话
        SqlSession s = sf.openSession();
        System.out.println(s);
        //实现删除操作
        //s.delete("deleteById",2);
        //实现查询操作 返回List结果
        List<Member> sts = s.selectList("query");
        System.out.println(sts.size());
        for (Member st : sts) {
            System.out.println(st.getName());
        }
        s.commit();
    }
}

 

mybatis入门实例

标签:resource   string   typealias   建立   parameter   code   ade   sre   --   

原文地址:http://www.cnblogs.com/fqszywz/p/7521687.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!