文件骨架
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" >
< environment id = " mysql" >
< transactionManager type = " JDBC" > </ transactionManager>
< dataSource type = " POOLED" >
< property name = " driver" value = " com.mysql.jdbc.Driver" />
< property name = " url" value = " jdbc:mysql://localhost:3306/glc" />
< property name = " username" value = " root" />
< property name = " password" value = " root" />
</ dataSource>
</ environment>
</ environments>
< mappers>
< mapper resource = " IUserDao.xml" />
</ mappers>
</ configuration>
IUserDao.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 = " glc.dao.UserDao" >
< select id = " findAll" resultType = " glc.domain.User" >
select * from user
</ select>
</ mapper>
java测试类
package glc. test;
import glc. dao. UserDao;
import glc. 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> users = userDao. findAll ( ) ;
for ( User user : users) {
System. out. println ( user) ;
}
session. close ( ) ;
in. close ( ) ;
}
}
UserDao接口
package glc. dao;
import glc. domain. User;
import java. util. List;
public interface UserDao {
List< User> findAll ( ) ;
}
log4j.properties
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# CONSOLE is set to be glc.开辟路径专用 ConsoleAppender using glc.开辟路径专用 PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
# LOGFILE is set to be glc.开辟路径专用 File appender using glc.开辟路径专用 PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
User(要求与Mysql属性一致)
package glc. domain;
import java. io. Serializable;
import java. util. Date;
public class User implements Serializable {
private Integer id;
private String username;
private Date birrhday;
private String sex;
private String address;
public User ( Integer id, String username, Date birrhday, String sex, String address) {
this . id = id;
this . username = username;
this . birrhday = birrhday;
this . sex = sex;
this . address = address;
}
public Integer getId ( ) {
return id;
}
public void setId ( Integer id) {
this . id = id;
}
public String getUsername ( ) {
return username;
}
public void setUsername ( String username) {
this . username = username;
}
public Date getBirrhday ( ) {
return birrhday;
}
public void setBirrhday ( Date birrhday) {
this . birrhday = birrhday;
}
public String getSex ( ) {
return sex;
}
public void setSex ( String sex) {
this . sex = sex;
}
public String getAddress ( ) {
return address;
}
public void setAddress ( String address) {
this . address = address;
}
@Override
public String toString ( ) {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birrhday=" + birrhday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}' ;
}
}
pom.xml(不同版本不同配置仅供参考)
<?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> org.example</ groupId>
< artifactId> SSM框架</ artifactId>
< version> 1.0-SNAPSHOT</ version>
< packaging> jar</ packaging>
< dependency> < groupId> org.mybatis</ groupId>
< artifactId> mybatis</ artifactId>
< version> 3.4.5</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 5.1.32</ version>
</ dependency>
< dependency>
< groupId> log4j</ groupId>
< artifactId> log4j</ artifactId>
< version> 1.2.12</ version>
</ dependency>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< version> 4.10</ version>
< scope> test</ scope>
</ dependency>
< repositories>
< repository>
< releases>
< enabled> true</ enabled>
</ releases>
< snapshots>
< enabled> false</ enabled>
</ snapshots>
< id> bintray.kotlin.eap</ id>
< name> Bintray Kotlin EAP Repository</ name>
< url> https://dl.bintray.com/kotlin/kotlin-eap</ url>
</ repository>
</ repositories>
< pluginRepositories>
< pluginRepository>
< releases>
< enabled> true</ enabled>
</ releases>
< snapshots>
< enabled> false</ enabled>
</ snapshots>
< id> bintray.kotlin.eap</ id>
< name> Bintray Kotlin EAP Repository</ name>
< url> https://dl.bintray.com/kotlin/kotlin-eap</ url>
</ pluginRepository>
</ pluginRepositories>
< build>
< plugins>
< plugin>
< executions>
< execution>
< id> compile</ id>
< phase> compile</ phase>
< goals>
< goal> compile</ goal>
</ goals>
</ execution>
< execution>
< id> test-compile</ id>
< phase> test-compile</ phase>
< goals>
< goal> test-compile</ goal>
</ goals>
</ execution>
</ executions>
< configuration>
< jvmTarget> 1.8</ jvmTarget>
</ configuration>
</ plugin>
< plugin>
< configuration>
< source> 13</ source>
< target> 13</ target>
</ configuration>
< executions>
< execution>
< id> compile</ id>
< phase> compile</ phase>
< goals>
< goal> compile</ goal>
</ goals>
</ execution>
< execution>
< id> testCompile</ id>
< phase> test-compile</ phase>
< goals>
< goal> testCompile</ goal>
</ goals>
</ execution>
</ executions>
</ plugin>
</ plugins>
</ build>
< build>
< finalName> mybatis-base</ finalName>
< resources>
< resource>
< directory> src/main/java</ directory>
< includes>
< include> **/*.xml</ include>
</ includes>
</ resource>
</ resources>
</ build>
< properties>
< mapper>
< mapper resource = " IUserDao.xml" > </ mapper>
</ mapper>
< project.build.sourceEncoding> UTF-8</ project.build.sourceEncoding>
< maven.compiler.encoding> UTF-8</ maven.compiler.encoding>
< java.version> 14</ java.version>
< maven.compiler.source> 14</ maven.compiler.source>
< maven.compiler.target> 14</ maven.compiler.target>
</ properties>
</ project>
成功运行!