记录一下在Maven中使用Mybaits
单纯的Mybatis,没有整合spring
首先在pom中导入依赖
点击查看代码
<dependencies>
<!-- 单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<!-- MyBatis核心-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.10</version>
</dependency>
<!-- MySql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<!-- log4j日志-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
使用MyBatis需要在resource文件夹中创建一个xml进行一下全局配置
MyBatis的xml约束头
右键new -> file
文件以.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">
点击查看代码
<?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="testDataSource">
<environment id="testDataSource">
<transactionManager type="jdbc"></transactionManager>
<dataSource type="pooled">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///test?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
<environment id="testDataSource2">
<transactionManager type="jdbc"></transactionManager>
<dataSource type="pooled">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///test?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
<package name=""/>
</mappers>
</configuration>
environments标签的default属性为子标签environment的Id
default是那个id就会使用那个数据库配置
mappers标签为映射配置文件的路径
标签有两种:
mapper 属性 resource 路径具体到文件名
package 属性 name 路径具体到放置文件的包名
typeAliases标签为取别名
映射配置文件UserMapper.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="com.wz.mapper.UserMapper">
<select id="queryList" resultType="com.wz.pojo.User">
select * from t_user
</select>
</mapper>
namespace属性为对应的接口类。
select标签代表查询,insert标签代表新增等等
resultType属性为返回值类型,如果全局配置中没有配置别名的话这里要写全限定类名,如果配置了别名写别名就可以了
接口类UserMapper
点击查看代码
public interface UserMapper {
public List<User> queryList();
}
方法类
点击查看代码
public class SqlSessionUtils {
private static SqlSession sqlSession;
public static SqlSession getSqlsession(){
try {
InputStream resource = Resources.getResourceAsStream("mysql_config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resource);
sqlSession = sessionFactory.openSession(true);
} catch (Exception e) {
throw new RuntimeException(e);
}
return sqlSession;
}
public static void closeSession(){
if (sqlSession != null){
sqlSession.close();
}
}
}
测试类
点击查看代码
public class mysqlTest {
@Test
public void test01(){
SqlSession sqlsession = SqlSessionUtils.getSqlsession();
UserMapper mapper = sqlsession.getMapper(UserMapper.class);
List<User> users = mapper.queryList();
for (User user : users) {
System.out.println(user);
}
}
}