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

(0)

相关推荐