MyBatis01 Idea中搭建MyBatis开发环境
项目结构
POM模板
<?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"> <parent> <artifactId>ssm</artifactId> <groupId>com.etc</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>chapter01</artifactId> <packaging>war</packaging> <name>chapter01 Maven Webapp</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> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> </dependencies> <build> <finalName>chapter01</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_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-war-plugin</artifactId> <version>3.2.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> <!-- 查看源代码 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.0.1</version> <executions> <execution> <id>attach-sources</id> <phase>verify</phase> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> <!-- 查看文档 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>3.0.1</version> <executions> <execution> <id>attach-javadocs</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> </plugins> </pluginManagement> <!--引入资源文件:包括dao下的mapper.xml文件--> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build> </project>
database.properties
driver=com.mysql.cj.jdbc.Driver url=jdbc\:mysql\://localhost:3306/#?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false&allowPublicKeyRetrieval=true username=# password=#
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> <!-- 引入数据库属性配置文件 --> <properties resource="database.properties"/> <!--<properties>--> <!--<property name="driver" value="com.mysql.jdbc.Driver" />--> <!--<property name="url" value="jdbc:mysql://localhost:3306/#" />--> <!--<property name="username" value="#" />--> <!--<property name="password" value="#" />--> <!--</properties>--> <!--配置mybatis的日志处理:Log4j --> <settings> <setting name="logImpl" value="LOG4J"/> </settings> <typeAliases> <!-- <typeAlias alias="user" type="com.etc.entity.User" /> --> <package name="com.etc.entity"/> </typeAliases> <!--设置mybatis的运行环境,通过过default指定其中一种环境--> <environments default="development"> <environment id="development"> <!--使用JDBC事务管理--> <transactionManager type="JDBC"/> <!--使用mybatis数据源POOLED--> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> <environment id="jnditest"> <!--使用JDBC事务管理--> <transactionManager type="JDBC"/> <!--使用JNDI数据源,JNDI需要在tomcat配置文件中进行配置--> <dataSource type="JNDI"> <property name="data_source" value="java:comp/env/jndi/mybatis"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/etc/dao/UserMapper.xml"/> </mappers> </configuration>
配置文件节点的出现有顺序,否则配置文件会报错。
mybaitis映射文件
<?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.etc.dao.UserMapper"> <!-- 统计用户表人数 --> <select id="count" resultType="int"> SELECT COUNT(1) as count FROM smbms_user </select> <select id="listUser" resultType="User"> SELECT * FROM smbms_user </select> </mapper>
DAO
package com.etc.dao; import java.util.List; import com.etc.entity.User; public interface UserMapper { /** 统计用户人数 */ int count(); /** 查询全部用户 */ List<User> listUser(); }
本博客文章未经许可,禁止转载和商业用途!
如有疑问,请联系: 2083967667@qq.com