pom.xml配置如下
<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.itcast.mybatis01</groupId>
<artifactId>mybatis01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!--导入mybatis依赖-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.7</version>
</dependency>
<!--需要用到mysql包依赖-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
<!--配置log4j。同时不要忘记建立log4j.properties-->
<dependency>
<groupId>org.bgee.log4jdbc-log4j2</groupId>
<artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
<version>1.16</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.13</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
</dependencies>
</project>
log.properties配置如下:
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
在资源目录下创建SqlMapConfig.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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/web_test3"/>
<property name="username" value="root"/>
<property name="password" value="admin"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper.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">
<!--在这里namespace="test" 随便写的,后面再研究-->
<mapper namespace="test">
<!--
resultType:表示我们的结果类型
parameterType:表示我们的参数的类型
在mybatis中使用#{}来做我们的占位符
-->
<select id="getUserNameByID" resultType="mybatis01.dao.User" parameterType="int">
select * from user where id = #{id}
</select>
<select id="getUserByAddress" resultType="mybatis01.dao.User" parameterType="string">
select * from user where nickname like '%${value}%'
</select>
</mapper>
根据数据表中数据属性创建Uer对象,并生成getter/setter方法
创建MybatisFirst类:
public class MybatisFirst {
private SqlSession openSession;
@Test
public void getUserNameByID() throws IOException {
User userObj = openSession.selectOne("getUserNameByID", 6);
System.out.println(userObj.getNickname());
}
// 注解Before :每次方法执行前都会执行这里
@Before
public void init() throws IOException {
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory build = builder.build(Resources.getResourceAsStream("SqlMapConfig.xml"));
openSession = build.openSession();
}
@Test
public void getUserByAddress()throws IOException {
List<User> userList = openSession.selectList("getUserByAddress", "花花");
for (User usrObj : userList) {
System.out.println(usrObj.getNickname());
}
}
}
运行即可
记得maven配置文件的镜像地址改成阿里云的。并修改自己本地仓库的保存路径;
maven阿里云镜像如下:
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>