总项目预览图:
1. 在Navicat Premium搭建实验数据库
CREATE TABLE `user` (
`id` int(20) NOT NULL,
`name` varchar(30) DEFAULT NULL,
`pwd` varchar(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into `user`(`id`,`name`,`pwd`) values (1,'狂神','123456'),(2,'张三','abcdef'),(3,'李四','987654');
2. 新建maven项目,并导入pom依赖
在pom中导入依赖:
mysql-connector-java 导入5点多,好像不行
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
2. Maven静态资源过滤问题——解决方法:
在父、子项目中的pom文件导入:
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
~~~~~~~~~~~~~ 在父项目中,新建一个子项目 (不分父、子项目就不需要)~~~~~~~~~~~~~
删除src目录,方便后面建立子工程
新建一个module
直接next,填写项目名
3. 测试数据库连接
参考我的博客:6. IDEA关联数据库(在界面的右边的Database)
4. 编写MyBatis核心配置文件:mybatis-config.xml
mybatis-config.xml 包含了对MyBatis系统的核心设置,包含获取数据库连接实例的数据源(DataSource) 和决定事务范围和控制方式的事务管理器(TransactionManager) 。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.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/2mybatis_first?userSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="xxxx'"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/dao/userMapper.xml"/>
</mappers>
</configuration>
5. 编写MyBatis工具类:utils包下的MybatisUtils.java类
public class MybatisUtils {
static SqlSessionFactory sqlSessionFactory = null;
static {
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
6. 创建实体类:User
public class User {
private int id;
private String name;
private String pwd;
}
7. 编写UserDao接口类
public interface UserDao {
public List<User> getUserList();
}
8. 编写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.dao.UserDao">
<select id="getUserList" resultType="com.pojo.User">
select * from USER
</select>
</mapper>
9. 编写测试类
public class UserDaoTest {
@Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao userDao = sqlSession.getMapper(UserDao.class);
List<User> userList = userDao.getUserList();
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
}
10. 运行测试,成功的查询出来的我们的数据,ok!
|