欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

MyBatis搭建項(xiàng)目

工具包:

創(chuàng)新互聯(lián)是專(zhuān)業(yè)的開(kāi)平網(wǎng)站建設(shè)公司,開(kāi)平接單;提供網(wǎng)站設(shè)計(jì)制作、網(wǎng)站設(shè)計(jì),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專(zhuān)業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行開(kāi)平網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專(zhuān)業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專(zhuān)業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!

Netbeans8

Maven

MyBatis

項(xiàng)目源碼:https://github.com/sun2shadow/simpleMybatis

  1. 使用netbeans創(chuàng)建項(xiàng)目,選擇文件->新建項(xiàng)目->Maven->Web應(yīng)用程序;

  2. 打開(kāi)項(xiàng)目目錄,在依賴關(guān)系上右擊->添加依賴關(guān)系->查詢框輸入MySQL->選擇mysql:mysql-connector-java->點(diǎn)開(kāi)選擇對(duì)應(yīng)的mysql驅(qū)動(dòng)版本.

  3. 創(chuàng)建數(shù)據(jù)庫(kù)和表

create database foretaste;
use foretaste
create table user_info(id int(11) not null primary key auto_increment,
 nickname varchar(50) not null, phone_num char(11) not null, 
 created_time timestamp not null default current_timestamp, 
 last_update_time timestamp not null default current_timestamp);

    4. 創(chuàng)建UserInfo的實(shí)體

package com.shadow.foretaste.entity;

import java.util.Date;

/**
 *
 * @author sunny
 */
public class UserInfo {
    private int id;
    private String nickname;
    private String phoneNum;
    private Date createdTime;
    private Date lastUpdateTime;

    public int getId() {
        return id;
    }

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

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getPhoneNum() {
        return phoneNum;
    }

    public void setPhoneNum(String phoneNum) {
        this.phoneNum = phoneNum;
    }

    public Date getCreatedTime() {
        return createdTime;
    }

    public void setCreatedTime(Date createdTime) {
        this.createdTime = createdTime;
    }

    public Date getLastUpdateTime() {
        return lastUpdateTime;
    }

    public void setLastUpdateTime(Date lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
    }

    @Override
    public String toString() {
        return "UserInfo[id=" + id;
    }
    
    
}

 5. 創(chuàng)建UserInfoDao,必須先創(chuàng)建一個(gè)Dao接口,用于mapper綁定時(shí)指明的namspace

package com.shadow.foretaste.dao;

import com.shadow.foretaste.entity.UserInfo;

/**
 *
 * @author sunny
 */
public interface UserInfoDao {
    
    /**
     * 根據(jù)Id查詢用戶信息
     * @param id
     * @return 
     */
    UserInfo getUserInfoById(int id);
}

6. 點(diǎn)開(kāi)->項(xiàng)目的其他源->src/main/source,在默認(rèn)包上右擊,xml文件,命名為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> 
   <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/foretaste?zeroDateTimeBehavior=convertToNull "/>  
                <property name="username" value="root"/>  
                <property name="password" value="mysql123"/>  
            </dataSource>  
        </environment>  
    </environments>      

    <mappers>
        <mapper resource="mapper/UserInfoMapper.xml" />
    </mappers>

</configuration>

7. 在source文件下,新建mapper文件夾,并創(chuàng)建UserInfoMapper.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.shadow.foretaste.UserInfoDao">
    <select id="getUserInfoById" parameterType="int" resultType="com.shadow.foretaste.entity.UserInfo">
        select * from user_info where id = #{id}
    </select>
</mapper>

8. 創(chuàng)建MyBatisUtils獲取sqlSession

package com.shadow.foretaste.util;

import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 *
 * @author sunny
 */
public class MyBatisUtils {
    private static SqlSessionFactory factory = null;
    
    //初始化session工廠
    public static void initFactory() throws Exception{
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        factory = new SqlSessionFactoryBuilder().build(inputStream);
    }
    /**
     * 獲取sqlSession會(huì)話
     * @return 
     */
    public static SqlSession getSession(){
        if(null == factory){
            try {
                initFactory();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return factory.openSession();
    }
}

8. 創(chuàng)建測(cè)試方法驗(yàn)證配置

import com.shadow.foretaste.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;

/**
 *
 * @author sunny
 */
public class TesMyBatis {
    @Test
    public void testMyBatis(){
        SqlSession session = MyBatisUtils.getSession();
        assertNotNull(session);
        if(session != null){
            session.close();
        }
    }
}

好了,到此myBatis的框架就搭建完畢了.

文章名稱(chēng):MyBatis搭建項(xiàng)目
URL鏈接:http://chinadenli.net/article28/ggpscp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站服務(wù)器托管網(wǎng)站內(nèi)鏈品牌網(wǎng)站建設(shè)云服務(wù)器手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司