这是mybatis最简单的入门

这里有一个demo#这是mybatis最简单的入门

使用的IDE为idea

是maven的哦
这篇只是很简单的一个查询demo
目标是ssm
先来pom文件—-这个不知道在网上哪里找的 <_>

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0modelVersion>
  <groupId>MyTestgroupId>
  <artifactId>MyTestartifactId>
  <packaging>warpackaging>
  <version>1.0-SNAPSHOTversion>
  <name>MyTest Maven Webappname>
  <url>http://maven.apache.orgurl>
  <properties>
    
    <spring.version>4.0.6.RELEASEspring.version>
    
    <mybatis.version>3.2.7mybatis.version>
    
    <slf4j.version>1.7.7slf4j.version>
    <logback.version>1.1.2logback.version>
  properties>
  <dependencies>
    
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-coreartifactId>
      <version>${spring.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webartifactId>
      <version>${spring.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-oxmartifactId>
      <version>${spring.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-txartifactId>
      <version>${spring.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <version>${spring.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>${spring.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-aopartifactId>
      <version>${spring.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <exclusions>
        <exclusion>
          <groupId>commons-logginggroupId>
          <artifactId>commons-loggingartifactId>
        exclusion>
      exclusions>
      <version>${spring.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-aopartifactId>
      <version>${spring.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-testartifactId>
      <version>${spring.version}version>
    dependency>
    

    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>${mybatis.version}version>
    dependency>
    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatis-springartifactId>
      <version>1.2.2version>
    dependency>
    
    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>5.1.31version>
    dependency>
    
    <dependency>
      <groupId>org.apache.commonsgroupId>
      <artifactId>commons-dbcp2artifactId>
      <version>2.0.1version>
    dependency>

    
    <dependency>
      <groupId>org.codehaus.jacksongroupId>
      <artifactId>jackson-mapper-aslartifactId>
      <version>1.9.13version>
    dependency>

    
    
    <dependency>
      <groupId>org.slf4jgroupId>
      <artifactId>slf4j-apiartifactId>
      <version>${slf4j.version}version>
    dependency>
    <dependency>
      <groupId>org.slf4jgroupId>
      <artifactId>jcl-over-slf4jartifactId>
      <version>${slf4j.version}version>
    dependency>
    
    
    <dependency>
      <groupId>ch.qos.logbackgroupId>
      <artifactId>logback-classicartifactId>
      <version>${logback.version}version>
    dependency>
    <dependency>
      <groupId>ch.qos.logbackgroupId>
      <artifactId>logback-coreartifactId>
      <version>${logback.version}version>
    dependency>

    <dependency>
      <groupId>org.logback-extensionsgroupId>
      <artifactId>logback-ext-springartifactId>
      <version>0.1.1version>
    dependency>
    

  dependencies>
  <build>
    <finalName>testfinalName>
    <plugins>
      <plugin>
        <groupId>org.mybatis.generatorgroupId>
        <artifactId>mybatis-generator-maven-pluginartifactId>
        <version>1.3.2version>
      plugin>
    plugins>
  build>
project>

工程目录,一个标准的web工程,这个没什么说的
这是mybatis最简单的入门_第1张图片

User的实体
,看了这个自己去建立表哈

package com.fei.model;

/**
 * Created by fei on 15-9-6.
 */
public class User {
    private Integer id;
    private String name;
    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return  "User [id=" + id + ", name=" + name + ", age=" + age + "]";
    }
}

mybatis的配置文件conf.xml



<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="root" />
            dataSource>
        environment>
    environments>

    <mappers>
        <mapper resource="mapper/userMapper.xml" />
    mappers>
configuration>

语句的映射文件,都能看得懂吧



<mapper namespace="com.fei.mapper.userMapper">
    <select id="getUserById" parameterType="int" resultType="com.fei.model.User">
      SELECT * FROM user WHERE id=#{id}
    select>
mapper>

,基本的就是这么多,我们就可以来使用了,这是我的main

package com.fei;


import com.fei.model.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * Created by fei on 15-9-6.
 */
public class Test {
    public static void main(String[] args) {
        String resource="conf.xml";
        InputStream in=Test.class.getClassLoader().getResourceAsStream(resource);
        SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession=sessionFactory.openSession();

        String stsm="com.fei.mapper.userMapper.getUserById";
        User u = sqlSession.selectOne(stsm,1);
        System.out.println(u.toString());
    }
}

这个不要有疑问了
这是mybatis最简单的入门_第2张图片

没了,就这么多,应该可以运行了吧

你可能感兴趣的:(mybatis)