精品伊人久久大香线蕉,开心久久婷婷综合中文字幕,杏田冲梨,人妻无码aⅴ不卡中文字幕

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
eclipse構建maven+spring+mybatis控制臺應用。

使用maven+spring+mybatis構建控制臺應用,方便調試spring或者mybatis代碼,或者編寫測試代碼。

一,首先使用eclipse構建maven項目。詳細教程。

二,在你的 pom.xml 文件<denpendencies></denpendencies>節點中加入如下引用信息。

<dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-context</artifactId>    <version>5.3.4</version></dependency><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-core</artifactId>    <version>5.3.4</version></dependency><dependency>    <groupId>org.mybatis</groupId>    <artifactId>mybatis</artifactId>    <version>3.5.4</version></dependency>

然后執行拉取命令:mvn install

二,在resource目錄下新建mybatis-config.xml文件。用于配置mybatis和連接數據庫。

<?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>            <settings>                          <!--將以下畫線方式命名的數據庫列映射到 Java 對象的駝峰式命名屬性中-->        <setting name= "mapUnderscoreToCamelCase" value="true" />    </settings>    <!--簡化類命名空間 -->   <!--  <typeAliases>        <package name="tk.mybatis.simple.model" />    </typeAliases> -->  <environments default="development">    <environment id="development">      <transactionManager type="JDBC"/>      <dataSource type="POOLED">        <property name="driver" value="com.mysql.cj.jdbc.Driver"/>        <property name="url" value="jdbc:mysql://localhost:3306/wxapp"/>        <property name="username" value="root"/>        <property name="password" value="0412"/>      </dataSource>    </environment>  </environments>    <mappers>        <!--常規做法-->        <!--<mapper resource="tk/mybatis/simple/mapper/CountryMapper.xml" />-->        <!--<mapper resource="tk/mybatis/simple/mapper/PrivilegeMapper.xml" />-->        <!--<mapper resource="tk/mybatis/simple/mapper/RoleMapper.xml" />-->        <!--<mapper resource="tk/mybatis/simple/mapper/RolePrivilegeMapper.xml" />-->        <!--<mapper resource="tk/mybatis/simple/mapper/UserMapper.xml" />-->        <!--<mapper resource="tk/mybatis/simple/mapper/UserRoleMapper.xml" />-->        <!--第二種做法-->        <!-- <package name="tk.mybatis.simple.mapper"/> -->                <mapper resource="userMapper.xml" />    </mappers></configuration>
View Code

另新建applicationContext.xml文件,用于配置spring beans。

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:context="http://www.springframework.org/schema/context"   xmlns:mvc="http://www.springframework.org/schema/mvc"   xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd   http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context.xsd   http://www.springframework.org/schema/mvc   http://www.springframework.org/schema/mvc/spring-mvc.xsd"><bean id="helloWorld" class="beans.helloWorld">    <property name = "who">    <value> hello world spring</value>    </property></bean></beans>
View Code

新建資源文件夾mappers,并新建userMapper.xml文件。

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper   PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"  "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"><mapper namespace="entity.myuser">    <select id="getName" resultType="String">        select role_name from t_role        where id= #{id}    </select></mapper>
View Code

三,編寫主函數測試:

package main;import java.io.IOException;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;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import beans.helloWorld;public class test {    public static void main(String[] args) throws IOException {                ApplicationContext context = new FileSystemXmlApplicationContext("src/main/resources/applicationContext.xml");                        helloWorld hel = (helloWorld) context.getBean("helloWorld");            hel.print();                                    /* mybatis ????*/            //????sql????            String  resources = "mybatis-config.xml"; //??????????                        InputStream is = Resources.getResourceAsStream(resources);                        SqlSessionFactory fac = new  SqlSessionFactoryBuilder().build(is);                        SqlSession sqls = fac.openSession();                        String name = sqls.selectOne("entity.myuser.getName", 1);                        System.out.println(name);                        sqls.close();                                    }}
View Code

注意:獲取spring的ApplicationContext 對象使用FileSystemXmlApplicationContext方法,是從應用根目錄尋找配置文件,需要填寫配置文件路徑。

ApplicationContext context = new FileSystemXmlApplicationContext("src/main/resources/applicationContext.xml");

而mybatis是從資源文件中尋找配置文件,所以不需要寫路徑。

String resources = "mybatis-config.xml"; 
InputStream is = Resources.getResourceAsStream(resources);

四,完整代碼地址:

gitee:https://gitee.com/zhangdabao2019/mvn_spring_mybatis.git

 

本站僅提供存儲服務,所有內容均由用戶發布,如發現有害或侵權內容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
SSM框架(一)
MyBatis3整合Spring3_SpringMVC
解決idea創建ssm項目找不到mybatis的mapper的xml文件問題
Mybatis中Mapper的方法為什么不能重載,你知道嗎?
MyBatis Spring整合示例
maven+springMVC+mybatis+junit詳細搭建過程
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯系客服!

聯系客服

主站蜘蛛池模板: 隆林| 阿荣旗| 清苑县| 康平县| 定州市| 普格县| 额尔古纳市| 石屏县| 乌拉特中旗| 新绛县| 新余市| 古丈县| 屯门区| 镇宁| 中超| 潞城市| 闵行区| 望江县| 安远县| 交城县| 拜城县| 玛多县| 堆龙德庆县| 香港 | 晋中市| 金山区| 漾濞| 独山县| 昌都县| 蓝田县| 永州市| 辉县市| 江都市| 满城县| 雷波县| 海宁市| 保定市| 东方市| 山东省| 杭锦旗| 江永县|