Mybatis初探之環境的配置_資料庫

來源:互聯網
上載者:User

很早之前就想學習mybatis,據說很多公司都使用這個架構。以前我都使用老掉牙的DBCP,Hibernate感覺太大,現

在來學學這個中性的架構。


首先是環境的配置,我使用maven建立項目,pom.xml檔案如下

<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.bird</groupId><artifactId>concursey</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>concursey</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.2.7</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.32</version></dependency></dependencies></project>


還得建立一個資料庫和對應的一張表做示範,這裡就不寫了,直接上對應的JavaBean

package com.bird.mybatis.bean;public class Users {private int id;private String name;private int age;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Users [id=" + id + ", name=" + name + ", age=" + age + "]";}}

然後要給對應的JavaBean建立對應的mapper.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"><mapper namespace="com.bird.mybatis.bean.userMapper"><!--根據ID擷取對應的值  --><select id="getUser" parameterType="int" resultType="com.bird.mybatis.bean.Users">select * from users where id = #{id}</select></mapper>


當然還有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><!-- development:開發模式     work:工作模式 --><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/mybatis" /><property name="username" value="root" /><property name="password" value="root" /></dataSource></environment></environments> <mappers><mapper resource="com/bird/mybatis/bean/userMapper.xml" /></mappers></configuration>

最後是測試代碼

package com.bird.mybatis.bean;import java.io.Reader;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class Test {public static void main(String[] args) throws Exception {String resource = "conf.xml";Reader reader = Resources.getResourceAsReader(resource);SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);SqlSession session = sessionFactory.openSession();String statement = "com.bird.mybatis.bean.userMapper.getUser";Users user = session.selectOne(statement, 1);System.out.println(user);}}

總體來說還是非常簡單的。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.