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>
<!--引入外部db.properties-->
<properties resource="db.properties">
<!--启动默认值属性-->
<!--
<property name="org.apache.ibatis.parsing.PropertyParser.enable-default-value" value="true"/>
-->
<!--如果在属性名中使用了 ":" 字符(如:db:username),或者在 SQL 映射中使用了 OGNL 表达式的三元运算符
(如: ${tableName != null ? tableName : 'global_constants'}),就需要设置特定的属性来修改分隔属性名和默认值的字符。例如: -->
<!--修改默认值的分隔符
<property name="org.apache.ibatis.parsing.PropertyParser.default-value-separator" value="?:"/>
-->
</properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${mysql.driver}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
mapper.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为映射的根节点-->
<!--mapper为映射的根节点,namespace指定Dao接口的完整类名
mybatis会依据这个接口动态创建一个实现类去实现这个结构,
而这个实现类是一个Mapper对象-->
<mapper namespace="org.itheima.pojo.UserMapper">
<!--id = “接口中的方法名”
parameterType = “传入的参数类型”
resultType = “返回实体类对象,使用包.类名”-->
<select id="findById" parameterType="int"
resultType="org.itheima.pojo.User">
select * from users where id = #{id}
</select>
<!--对象中的属性可以直接取出-->
<insert id="addUser" parameterType="org.itheima.pojo.User">
insert into test.users(id,name,age) values (#{id},#{name},#{age});
</insert>
<delete id="deleteAUser" parameterType="int">
delete from users where id = #{id}
</delete>
<update id="updateAUser" parameterType="org.itheima.pojo.User">
update users set age = #{age} , name = #{name} where id = #{id}
</update>
<select id="findAllUser" resultType="org.itheima.pojo.User">
select * from users
</select>
<!--
<select id="findUsers" resultType="user">
select * from users
</select> -->
</mapper>
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>com.ithema</groupId>
<artifactId>MybatiesTest</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
</properties>
<dependencies>
<!--mybaties-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
db.properties
mysql.driver=com.mysql.cj.jdbc.Driver
mysql.username=
mysql.password=
mysql.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false&allowPublicKeyRetrieval=true
Comments | 2 comments
牛蛙牛蛙
@Yi