mybatis.xml主配置文件 pom文件 以及Mybatisutilswenjian

mybatis.xml

<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="mydey">
        <environment id="mydey">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/shuju"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/bjpowernode/dao/StudentDao.xml"/>
    </mappers>
</configuration>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
        <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>ch-1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>
    <dependencies>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</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>8.0.23</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>
    <plugins>
    <plugin>
    <artifactId>maven-compiler-plugin </artifactId>
    <version>3.1</version>
    <configuration>
    <source>11 </source>
    <target>11</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

Mybatisutils.java

package com.bjpowernode.untils;

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.IOException;
import java.io.InputStream;

public class MyBatisUntils {

    private  static SqlSessionFactory factory=null;
    static {
        String config= "mybatis.xml";

        InputStream in = null;
        try {
            in = Resources.getResourceAsStream(config);
            factory=new SqlSessionFactoryBuilder().build(in);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    //获取sqlsession的方法
    public static SqlSession getSqlSession(){
        SqlSession sqlSession=null;
        if ( factory!=null){
          sqlSession=  factory.openSession(true);//自动提交事务

        }
        return sqlSession;
    }
}

Student.xml 就是俗说的mapper文件,写sql语句的

<?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.bjpowernode.dao.StudentDao">


    <select id="selectStudents" resultType="com.bjpowernode.domain.Student">
       select id,name,email,age from student order by id
    </select>
    <insert id="insertStudent">
        insert into student values(#{id},#{name},#{email},#{age})
   <!-- 单个参数-->
    </insert>
    <select id="selectStudentsById" parameterType="java.lang.Integer" resultType="com.bjpowernode.domain.Student">
        select id,name,email,age from student where id=#{id}
    </select>
    <!--多个参数  使用@Param命名-->
<select id="selectMulitParam" resultType="com.bjpowernode.domain.Student">

select id,name,email,age from student where name=#{myname} or age=#{myage}

</select>


    <!--多个参数 ,使用java对象的属性值,作为参数实际值
    适用对象语法:#{属性名,javaType=类型名称,jdbcType=数据类型} 很少用
    javaType:指Java中的属性数据类型
    jdbcType:在数据库中的数据类型
    例如#{ paramName,javaType=java.lang.String,jdbcType=varchar}
   我们使用的是简化的方式 #{属性名},javaType的值mybatis反射能直接获取,不用提供
    -->
<!--    <select id="selectMultiObject" resultType="com.bjpowernode.domain.Student">-->
<!--        select id,name,email,age from student where name=#{paraName,javaType=java.lang.String,jdbcType=VARCHAR}-->
<!--        or age=#{paramAge,javaType=java.lang.Integer,jdbcType=Integer}-->

<!--    </select>-->
    <select id="selectMultiObject" resultType="com.bjpowernode.domain.Student">
        select id,name,email,age from student where name=#{paraName}
        or age=#{paramAge}
    </select>
<!--多个参数使用位置-->
    <select id="selectMultiPostition" resultType="com.bjpowernode.domain.Student">
    select   id,name,email,age from student where name=#{arg0} or age=#{arg1}

    </select>
    <!--map传参数-->
    <select id="selectMultiMap" resultType="com.bjpowernode.domain.Student">
        select   id,name,email,age from student where name=#{myname} or age=#{age}
    </select>
    <!--
    $替换列名
    -->
    <select id="selectUse$Oder" resultType="com.bjpowernode.domain.Student">
            select * from student order by ${colName}
    </select>

</mapper>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值