Mybatis配置及如何使用

配置pom.xml:



    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>


    <dependencies>
        <!--Junit依赖-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!--mybatis依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.1</version>
        </dependency>
        <!--mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.9</version>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory><!--所在的目录-->
                <includes><!--包括目录下的.properties.xml 文件都会扫描到-->
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

配置MyBatis-Config.xml(resources目录下):

<configuration>
    <!--配置 mybatis 环境--><!-- 引入jdbc.properties文件 -->
    <properties resource="jdbc.properties"/>

    <environments default="mysql">
        <!--id:数据源的名称-->
        <environment id="mysql">
            <!--配置事务类型:使用 JDBC 事务(使用 Connection 的提交和回滚)-->
            <transactionManager type="JDBC"/>
            <!--数据源 dataSource:创建数据库 Connection 对象
            	type: POOLED 使用数据库的连接池
            -->
            <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>
        <!--告诉 mybatis 要执行的 sql 语句的位置-->
        <mapper resource="com/xxx/dao/xxxxxDao.xml"/>
        <!--mapper的全限定名称-->
    </mappers>
</configuration>

配置jdbc.properties(resources目录下):

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123

配置utils.Myutil类:

public class MyBatisUtil {
    private static SqlSessionFactory factory;
    static{
        try {
            //1.定义mybatis主配置文件的名称,从类路径的根开始(target/clasess)
            String config = "MyBatis-Config.xml";
            //2.读取配置文件
            InputStream is = Resources.getResourceAsStream(config);
            //3.创建了SqlSessionFactoryBuilder对象,目的是获取SqlSession
            factory = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            factory = null;
            e.printStackTrace();
        }
    }
    /**
     * 获取SqlSession对象
     * @return SqlSession对象,获取失败则返回null
     */
    public static SqlSession getSqlSession()  {
        if (factory != null){
            //4.获取SqlSession,SqlSession能执行sql语句
            return factory.openSession();
        }
        return null;
    }
}

测试方法

public class test_01 {
    @Test
    public void test() {

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        assert sqlSession != null;
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        //用代理Dao类查询全部学生
        List<Student> students = dao.selectStudents();
        for (Student student : students) {
            System.out.println(student);
        }
    }
}

方法有多个参数

public List<Student> selectMultiParam(@Param("studentName") String name,@Param("studentAge") int age);

mapper文件

<select id="selectMultiParam" resultType="com.atguigu.pojo.Student">
        select id,name,email,age from student where name = #{studentName} or age = #{studentAge}
</select>
/**
     * 通过name或age查询学生(多个参数-使用对象)
     * @param queryParam 
     * @return 学生信息的List集合
     */
    public List<Student> selectMultiObject(QueryParam queryParam);
<select id="selectMultiObject" resultType="com.atguigu.pojo.Student">
<!--        方法名                           返回值对象的全限定名称-->
        select id,name,email,age from student where name=#{queryName} or age = #{queryAge}
</select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值