依赖
导入mybatis所需要的依赖
11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.ibatis/ibatis-core -->
<dependency>
<groupId>org.apache.ibatis</groupId>
<artifactId>ibatis-core</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.9</version>
</dependency>
<!-- http://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<!-- http://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.14</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.14</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
<resources>
<resource>
<directory>${basedir}/src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
实体类
查看我们需要查询的表
创建实体类
package com.mybatis.entity;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
public class Exercise {
private static final long seriaVersionID = 1l;
private Integer id;
private String stu_name;
private Double limit_time;
private String danwei;
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
private Date start_time;
private Date end_time;
@Override
public String toString() {
return "Exercise [id=" + id + ", stu_name=" + stu_name + ", limit_time=" + limit_time + ", danwei=" + danwei
+ ", start_time=" + start_time + ", end_time=" + end_time + "]";
}
public void set(Integer id, String stu_name, Double limit_time, String danwei, Date start_time, Date end_time) {
this.id = id;
this.stu_name = stu_name;
this.limit_time = limit_time;
this.danwei = danwei;
this.start_time = start_time;
this.end_time = end_time;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStu_name() {
return stu_name;
}
public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
public Double getLimit_time() {
return limit_time;
}
public void setLimit_time(Double limit_time) {
this.limit_time = limit_time;
}
public String getDanwei() {
return danwei;
}
public void setDanwei(String danwei) {
this.danwei = danwei;
}
public Date getStart_time() {
return start_time;
}
public void setStart_time(Date start_time) {
this.start_time = start_time;
}
public Date getEnd_time() {
return end_time;
}
public void setEnd_time(Date end_time) {
this.end_time = end_time;
}
public static long getSeriaversionid() {
return seriaVersionID;
}
}
Dao层
接口层
package com.mybatis.anno.dao;
import java.util.List;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import com.mybatis.entity.Exercise;
public interface ExecriseDao {
@Select("SELECT * FROM exercise")
List<Exercise> getAll();
}
配置文件
将dao加载到配置文件里面
<?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>
<settings>
<!--setting控制Mybatis全局行为的,设置输出日志 -->
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<environments default="mybatis">
<!--id是唯一值 -->
<environment id="mybatis">
<!--mybatis的事务类型 -->
<transactionManager type="JDBC" />
<!--数据源 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper class="com.mybatis.anno.dao.ExecriseDao" />
</mappers>
</configuration>
测试
package com.mybatis.anno.dao;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 com.mybatis.entity.Exercise;
public class ExerciseAnnoDaoTest {
public static void main(String[] args) throws Exception {
String resource = "mybatis/mybatis-config-anno.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
List<Exercise> exercises = session.selectList("com.mybatis.anno.dao.ExecriseDao.getAll");
for (Exercise exercise : exercises) {
System.out.println(exercise);
}
session.commit();
// 4. 关闭 Session
session.close();
}
}
结果