zoukankan      html  css  js  c++  java
  • 03mybatis-注解方式简单入门实例

    1、使用Idea 建立 java 基于maven的项目 jar

    2、maven项目的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>myb01</artifactId>
    <packaging>jar</packaging>
    <version>1.0</version>
    <dependencies>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.4</version>
    </dependency>
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.41</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>

    3、在项目src/main/resources/mybatis-conf.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/db?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8&amp;zeroDateTimeBehavior=convertToNull" />
    <property name="username" value="root" />
    <property name="password" value="root" />
    </dataSource>
    </environment>
    </environments>
    </configuration>

    4、数据库信息
    数据库名称为 db 账号 root 密码 root 端口3306 表数据信息
    mysql> select * from student;
    +--------+--------+--------+----------+
    | sid | sname | sscore | saddress |
    +--------+--------+--------+----------+
    | 201601 | 李四 | 90 | 郑州 |
    +--------+--------+--------+----------+

    5、编写Student.java
    package com.fz.entity;

    /**
    * Created by webrx on 2017-06-10.
    */
    public class Student {
    private int sid;
    private String sname;
    private int sscore;
    private String saddress;

    public int getSid() {
    return sid;
    }

    public void setSid(int sid) {
    this.sid = sid;
    }

    public String getSname() {
    return sname;
    }

    public void setSname(String sname) {
    this.sname = sname;
    }

    public int getSscore() {
    return sscore;
    }

    public void setSscore(int sscore) {
    this.sscore = sscore;
    }

    public String getSaddress() {
    return saddress;
    }

    public void setSaddress(String saddress) {
    this.saddress = saddress;
    }
    }

    6、编写StudentDAO 接口
    package com.fz.dao;

    import com.fz.entity.Student;
    import org.apache.ibatis.annotations.Insert;
    import org.apache.ibatis.annotations.Select;

    import java.util.List;

    /**
    * Created by webrx on 2017-06-10.
    */
    public interface StudentDAO {
    @Select("select * from student")
    public List<Student> queryAll();

    @Insert("insert into student(sname,sscore,saddress) values(#{sname},#{sscore},#{saddress})")
    public int insert(Student student);
    }

    7、编写测试程序 src/text/java/com/Demo.java
    package com;

    import com.fz.dao.StudentDAO;
    import com.fz.entity.Student;
    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 org.junit.Test;

    import java.io.IOException;
    import java.util.List;

    /**
    * Created by webrx on 2017-06-10.
    */
    public class Demo {
    @Test
    public void aaa() throws IOException {
    //建立
    SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
    sf.getConfiguration().addMapper(StudentDAO.class);

    SqlSession session = sf.openSession();

    StudentDAO sdao = session.getMapper(StudentDAO.class);
    //数据插入
    Student st = new Student();
    st.setSname("李四四");
    st.setSscore(88);
    st.setSaddress("郑州市文化路");

    sdao.insert(st);

    //查询显示数据
    List<Student> list = sdao.queryAll();
    for(Student stu : list){
    System.out.println(stu.getSname());
    }
    session.close();

    }
    }

    怕什么真理无穷,进一步有一步的欢喜
  • 相关阅读:
    大型电商业务架构 IT大咖说
    携程开源配置中心Apollo的设计与实现 IT大咖说
    李善友《认知升级之第一性原理》--507张PPT全解!_搜狐科技_搜狐网
    http://www.educity.cn/luntan/144478_5.html
    微服务架构下的分布式数据存储-技术之家
    http://blog.longjiazuo.com/archives/3080
    实施微服务架构的关键技术
    微服务架构的分布式事务解决方案
    k8s~helm镜像版本永远不要用latest
    nginx~对没有定义service_name的三级域名进行过滤
  • 原文地址:https://www.cnblogs.com/Mkady/p/7001683.html
Copyright © 2011-2022 走看看