MyBatis学习笔记

这篇博客详细介绍了MyBatis的配置过程,包括database.properties文件中的数据库连接信息,mybatis-config.xml中的全局设置、类型别名、数据源配置以及Mapper XML文件中的SQL语句定义。同时,展示了Test类中如何初始化SqlSessionFactory和执行Mapper接口的方法。内容涵盖了从数据库连接到SQL操作的完整流程。
摘要由CSDN通过智能技术生成

MyBatis

database.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://ip:3306/database?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
user=root
password=root

mybatis-config.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="database.properties"></properties>
	<settings>
		<setting name="logImpl" value="STDOUT_LOGGING"/>
	</settings>
	<typeAliases>
		<package name="实体类包"></package>
	</typeAliases>
	<environments default="default">
        <environment id="default">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"></property>
                <property name="url" value="${url}"></property>
                <property name="username" value="${user}"></property>
                <property name="password" value="${password}"></property>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/*.xml"></mapper>
    </mappers>
</configuration>

Dao.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="nj.zb.kb15.dao.RoleDao">

	<insert id="">
        insert into table_name
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="condition">

			</if>
		</trim>
        values
        <foreach collection="" item="" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
				<if test="condition">
					#{}
				</if>
			</trim>
        </foreach>
    </insert>

	<update id="">
		update table_name
		<set>
			<if test="condition">
				=#{}
			</id>
		</set>
		where =#{}
	</update>

	<update id="">
		<foreach collection="" item="">
			update table_name
			<set>
				<if test="condition">
					=#{}
				</id>
			</set>
			where =#{}
		</foreach>
	</update>

	<delete id="">
		delete from table_name where id in
		<foreach collection="" item="" open="(" close=")" separator=",">
			#{}
		</foreach>
	</delete>

	<sql id="">
		SQL语句
	</sql>

	<resultMap id="" type="">
		<id property="" column=""></id>
		<result property="" column=""></result>
		<collection property="" ofType=""> //一对多
			<id property="" column=""></id>
			<result property="" column=""></result>
		</collection>
		<association property="" javaType="">  //一对一
			<id property="" column=""></id>
			<result property="" column=""></result>
		</association>
	</resultMap>

	<select id="" resultMap="resultMap"/resultType="">
		<include refid="sql"></include>
		<where>
			<if test="condition">

			</if>
		</where>
		<choose>
			<when test="condition">

			</when>
			<otherwise>

			</otherwise>
		</choose>
	</select>
</mapper>

Test类

public class Test{
	SqlSessionFactory sessionFactory=null;
    SqlSession sqlSession=null;
    RoleDao roleDao=null;
    UserDao userDao=null;

    @Before
    public void start(){
        InputStream is = TestRole.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
        sessionFactory = new SqlSessionFactoryBuilder().build(is);
        sqlSession=sessionFactory.openSession();
        roleDao=sqlSession.getMapper(RoleDao.class);
        userDao=sqlSession.getMapper(UserDao.class);
    }

    @Test
    public void test(){
    	Dao dao=new Dao();
    	dao.方法名();
    }

    @After
    public void end(){
        sqlSession.commit();
        sqlSession.close();
        System.out.println("over");
    }
}

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>nj.zb.kb15</groupId>
  <artifactId>mybatis_stu</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>mybatis_stu</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

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

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.2.2</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.25</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

honconM

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值