首先引入数据库连接的依赖:
<!--数据库依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.17</version>
</dependency>
<!--数据库连接-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.13</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
编写Mybatis配置文件(mybatis-config.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>
<properties resource="jdbc.properties"/>
<typeAliases>
<package name="org.circle.domain"/>
</typeAliases>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driverName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="org.circle.dao"/>
</mappers>
</configuration>
编写MySQL数据库连接数据信息(jdbc.properties)
driverName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/usertest
username=root
password=root
name=circle
编写spring的配置文件(application)
<context:component-scan base-package="org.circle"/>
是Spring Framework中的一个配置项,它用于自动扫描指定包及其子包下的所有类,并根据类的注解自动装配到Spring容器中。
具体来说,base-package
属性指定了要扫描的包的根目录, Spring会遍历该包及其子包下的所有类文件,如果发现其中使用了特定注解(如@Component
,@Service
,@Controller
等),就会自动将这些类实例化并添加到Spring容器中,以便对其进行管理。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd"
>
<!--扫描包-->
<context:component-scan base-package="org.circle"/>
</beans>
编写dao层(StudentDao.java)
public interface StudentDao {
@Select("select * from student where id = #{id}")
Student findById(Integer id);
}
编写实体类(Student.java)
public class Student {
private Integer id;
private String name;
private Integer age;
private String sex;
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
}
编写主测试文件(MybatisMain.java)
public class MybatisMain {
public static void main(String[] args) throws IOException {
//创建SqlSessionFactoryBuild对象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
//加载SqlMapConfig配置文件
InputStream inputStream = Resources.getResourceAsStream("mybaties-config.xml");
//创建SqlSessionFactory对象
SqlSessionFactory build = sqlSessionFactoryBuilder.build(inputStream);
//获取SqlSession
SqlSession sqlSession = build.openSession();
//执行SqlSession对象执行查询,获取结果Student
StudentDao mapper = sqlSession.getMapper(StudentDao.class);
//查询
Student student = mapper.findById(1);
System.out.println(student);
//释放资源
sqlSession.close();
}
}