1. 创建Maven Project,选择war,修改pom.xml
<properties> <!-- JDK版本 --> <java.version>1.8</java.version> <!-- mybatis版本 --> <mybatis.version>3.5.2</mybatis.version> <!-- mysql driver 版本 --> <mysql.version>5.1.48</mysql.version> </properties> <dependencies> <!-- web project基本配置 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- mybatis 核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- asm --> <dependency> <groupId>asm</groupId> <artifactId>asm</artifactId> <version>3.3.1</version> </dependency> <!-- cglib --> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.2.7</version> <exclusions> <exclusion> <groupId>org.ow2.asm</groupId> <artifactId>asm</artifactId> </exclusion> </exclusions> </dependency> <!-- javassist --> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.23.1-GA</version> </dependency> <!-- log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- log4j-api --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.11.1</version> </dependency> <!-- slf4j-api --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.28</version> </dependency> <!-- slf4j-log4j12 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.28</version> <scope>test</scope> </dependency> <!-- mysql driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> </dependencies> <build> <!-- 资源拷贝,默认不会将xml文件编译到classpath下 --> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </resource> </resources> </build>
2. 在src/main/resources目录下创建mybatis.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> <!-- mybatis使用properties来引入外部properties配置文件的内容 resource 引入类路径下资源 url 引入网络路径或磁盘路径下资源 --> <properties resource="jdbc.properties"></properties> <!-- default指定的是当前使用的环境 --> <environments default="default"> <!-- 声明可能使用到的环境 --> <environment id="default"> <!-- 使用原生的JDBC事务,默认是自动提交的 --> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.name}" /> <property name="password" value="${jdbc.password}" /> </dataSource> </environment> </environments> </configuration>
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://192.168.178.5:12345/ssm?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai jdbc.name=root jdbc.password=123456
3. 创建映射文件
(1) 实体类
public class User { private Integer userId; private String userName; private Integer age; public User() { } public User(Integer userId, String userName, Integer age) { this.userId = userId; this.userName = userName; this.age = age; } public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User [userId=" + userId + ", userName=" + userName + ", age=" + age + "]"; } }
(2) 映射接口
public interface UserMapper { public List<User> selectAll(); }
(3) xml映射文件(可以看做是UserMapper接口的实现类)
<?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"> <!-- namespace对象接口的全限定名称(包名+类名) --> <mapper namespace="com.linhw.demo.mapper.UserMapper"> <!-- id对应接口的方法名称 --> <select id="selectAll" resultMap="UserInfo"> select * from user </select> <!-- 数据列与实体属性的映射 --> <resultMap type="com.linhw.demo.pojo.User" id="UserInfo"> <id column="user_id" property="userId" jdbcType="INTEGER"/> <result column="user_name" property="userName" jdbcType="VARCHAR"/> <result column="age" property="age" jdbcType="INTEGER"/> </resultMap> </mapper>
(4) 修改mybatis.xml,配置映射文件的路径
放在配置文件所有标签的最后。
<!-- 配置映射文件 --> <mappers> <mapper resource="com/linhw/demo/mapper/UserMapper.xml"/> </mappers>
4. 测试
//加载全局配置文件 InputStream inputStream = Resources.getResourceAsStream("mybatis.xml"); //使用工厂设计模式 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //生产SqlSession SqlSession session = sessionFactory.openSession(); List<User> userList = session.selectList("com.linhw.demo.mapper.UserMapper.selectAll"); for(User user : userList){ System.out.println(user.toString()); } //关闭连接 session.close();
附:xml配置文件自动提示
在全局配置文件或者mapper映射文件的约束中点击,电脑会提示下载文件到本地:
config:http://mybatis.org/dtd/mybatis-3-config.dtd
mapper:http://mybatis.org/dtd/mybatis-3-mapper.dtd
配置eclipse:打开window–>Preferences–>XML–>XML catalog
点击add,Location地方找到你下载好的dtd文件,key type用System-ID,key为http://mybatis.org/dtd/mybatis-3-config.dtd 或 http://mybatis.org/dtd/mybatis-3-mapper.dtd,然后确定保存,再返回xml文件,你就会发现自动提示能用啦(如果没有提示,需要重新选择xml文件的开发方式Open With Xml Editor)。