91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

01.基于IDEA搭建mybatis的MAVEN項目

發布時間:2020-07-25 03:14:18 來源:網絡 閱讀:1303 作者:IMUKL8 欄目:軟件技術

01.基于IDEA搭建mybatis的MAVEN項目

<br/>

基礎環境

<br/>

  • IDEA
    <br/>
  • MAVEN

<br/>

工程目錄

<br/>
01.基于IDEA搭建mybatis的MAVEN項目

<br/>

mysql數據庫

<br/>
01.基于IDEA搭建mybatis的MAVEN項目
<br/>

Maven Pom

添加mybatis以及mysql的依賴包

<!--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.mu</groupId>
    <artifactId>mybatis</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.29</version>
        </dependency>
    </dependencies>
    <build>
        <!--idea不會尋找src/main/java目錄下的xml配置-->
        <resources>
           <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.tld</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.tld</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>
</project>

創建各實體類以及mapper.xml文件

<br/>

  • User.java
package com.mu.mybatis.domain;

/**
 * Created by manunited1985 on 2017/11/23.
 */
public class User {
    private Integer userId;
    private String userName;
    private String userPassword;
    private String userEmail;

    public User(Integer userId, String userName, String userPassword, String userEmail) {
        this.userId = userId;
        this.userName = userName;
        this.userPassword = userPassword;
        this.userEmail = userEmail;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public String getUserEmail() {
        return userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    @Override
    public String toString() {
        return "User [userId=" + userId + ", userName=" + userName
                + ", userPassword=" + userPassword + ", userEmail=" + userEmail
                + "]";
    }
}

<br/>

  • UserMapper.java
package com.mu.mybatis.mapper;
import com.mu.mybatis.domain.User;

/**
 * Created by manunited1985 on 2017/11/24.
 */
public interface UserMapper {
    public User selectUserById(Integer userId);
    public List<User> selectUserAll();
    public void insertUser(User user);
}

<br/>

  • UserMapper.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.mu.mybatis.mapper.UserMapper">
    <!--設置domain類和數據庫中表的字段一一對應,注意數據庫字段和domain類中的字段名稱不致,此處一定要!-->
    <resultMap id="BaseResultMap" type="com.mu.mybatis.domain.User">
        <id column="USER_ID" property="userId" jdbcType="INTEGER" />
        <result column="USER_NAME" property="userName" jdbcType="CHAR" />
        <result column="USER_PASSWORD" property="userPassword" jdbcType="CHAR" />
        <result column="USER_EMAIL" property="userEmail" jdbcType="CHAR" />
    </resultMap>
    <!-- 查詢單條記錄 -->
    <select id="selectUserById" parameterType="int" resultMap="BaseResultMap">
        SELECT * FROM t_user WHERE USER_ID = #{userId}
    </select>
         <!-- 查詢所有記錄 -->
    <select id="selectUserAll" resultMap="BaseResultMap">
        SELECT * FROM t_user
    </select>
     <!-- 插入單條記錄 -->
    <insert id="insertUser" parameterType="com.mu.mybatis.domain.User">
        insert into t_user (USER_ID,USER_NAME,USER_PASSWORD,USER_EMAIL)
        values (#{userId},#{userName},#{userPassword},#{userEmail})
    </insert>
</mapper>

<br/>

  • Main.java
package com.mu.mybatis;

import com.mu.mybatis.domain.User;
import com.mu.mybatis.mapper.UserMapper;
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.InputStream;
import java.io.IOException;
import java.util.List;

/**
 * Created by manunited1985 on 2017/11/23.
 */
public class Main {

    public static void main(String[] args) throws IOException {

        SqlSession session = null;

        try {
            // 讀取配置信息
            String resource = "application.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

            // 選擇不同的環境
            String env = "product";
            SqlSessionFactory factory = builder.build(inputStream,env);

            // 產生SqlSession對象
            session = factory.openSession();
            System.out.println(session);

            // 獲取映射對象            
            UserMapper userMapper = session.getMapper(UserMapper.class);

            // 獲取id為1的賬戶
            User user = userMapper.selectUserById(1);
            System.out.println(user);

            // 獲取所有賬戶
            List<User> arrayList = userMapper.selectUserAll();
            System.out.println(arrayList);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (session != null)
                // 關閉連接
                session.close();
        }

    }
}

<br/>

數據庫配置 jdbc.properties

jdbc_driver=com.mysql.jdbc.Driver
jdbc_url=jdbc\:mysql\://127.0.0.1\:3306/test?useUnicode=true&characterEncoding=utf-8
jdbc_username=root
jdbc_password=123456

<br/>

應用配置application.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>
    <properties resource="jdbc.properties"></properties>
    <environments default="development">
        <!--配置不同的環境下的數據源-->
        <!--開發模式下-->
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc_driver}"/>
                <property name="url" value="${jdbc_url}"/>
                <property name="username" value="${jdbc_username}"/>
                <property name="password" value="${jdbc_password}"/>
            </dataSource>
        </environment>
        <!--生產模式下-->
        <environment id="product">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc_driver}"/>
                <property name="url" value="${jdbc_url}"/>
                <property name="username" value="${jdbc_username}"/>
                <property name="password" value="${jdbc_password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/mu/mybatis/mapper/UserMapper.xml"/>
    </mappers>
</configuration>

<br/>

運行結果

org.apache.ibatis.session.defaults.DefaultSqlSession@454b70a9
User [userId=1, userName=xiaoming, userPassword=123456, userEmail=x@sina.com]
[User [userId=1, userName=xiaoming, userPassword=123456, userEmail=x@sina.com], User [userId=2, userName=tom, userPassword=123, userEmail=tom@sina.com], User [userId=3, userName=jack, userPassword=123, userEmail=jack@sina.com]]

備注

如果你利用IDEA創建的工程,在加載mapper.xml文件的時候,出現類似現象:

mybatis錯誤——java.io.IOException: Could not find resource com/xxx/xxxMapper.xml

那么,你可以在pom.xml文件的添加如下字段:

 <build>
        <!--idea不會尋找src/main/java目錄下的xml配置-->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

IDEA是不會編譯src的java目錄的xml文件,所以在Mybatis的配置文件中找不到xml文件!(也有可能是Maven構建項目的問題,網上教程很多項目是普通的Java web項目,所以可以放到src下面也能讀取到)

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

资中县| 彭阳县| 无棣县| 炎陵县| 屏边| 宁安市| 攀枝花市| 遂宁市| 虎林市| 大同县| 虹口区| 延川县| 丹棱县| 惠安县| 行唐县| 无为县| 犍为县| 渭源县| 阿鲁科尔沁旗| 甘南县| 鄂州市| 宜君县| 青海省| 阿城市| 濮阳县| 金阳县| 蒙自县| 波密县| 葵青区| 元谋县| 平邑县| 山东省| 滕州市| 拉萨市| 铅山县| 景东| 徐闻县| 嘉祥县| 沁源县| 黔南| 沁阳市|