基于maven创建mybatis项目
文章目录
写在最前
1.项目目录如下所示:
![](https://i-blog.csdnimg.cn/blog_migrate/43d06e6daf9f5e1a699e90b5ecf345d5.png)
2.需先在数据库中建表account
account表中属性为id,name,money即可。
壹 | 环境搭建
1. 创建maven项目。
不用骨架,直接next,填项目名称(com.try mybatistry01)等,然后finish。
2. 在pom.xml中添加坐标依赖:
**添上jar的打包方式,以及添加mybatis、junit、mysql、log4j的坐标。
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.try</groupId>
<artifactId>mybatistry1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
</dependencies>
</project>
贰 | 编写代码
1.编写User实体类
package com.domain;
import java.io.Serializable;
public class User implements Serializable{
private Integer id;
private String name;
private Double money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
2.编写持久层接口userDao
package com.dao;
import com.domain.User;
import java.util.List;
public interface userDao {
/**
* 查询所有
* @return
*/
List<User> findAll();
}
3.编写持久层接口的映射文件userDao.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="findAll" resultType="com.domain.User">
SELECT * FROM account;
</select>
</mapper>
4.编写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="mysql">
//注意:environments标签中的default必须和environment中的id相同
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql:///ssm"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
//告知mybatis映射配置的位置
<mappers>
<mapper resource="com/dao/userDao.xml" />
</mappers>
</configuration>
5.编写测试类mybatisTest
import com.dao.userDao;
import com.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
import java.util.List;
public class mybatisTest {
public static void main(String[] args) throws Exception{
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
SqlSession session = factory.openSession();
userDao userdao = session.getMapper(userDao.class);
List<User> list = userdao.findAll();
for (User user : list)
{
System.out.println(user);
}
session.close();
in.close();
}
}
叁 | 成功运行
1.如果想让log4j的warn去掉的话可以导入log4j的包。
![](https://i-blog.csdnimg.cn/blog_migrate/6493c36cb5df5ce52710d9032bbda7c5.png)