mybatis-introduction-annotation
1.pom
< ? 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. lq< / groupId> < artifactId> mybatis- introduce< / 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> mysql< / groupId> < artifactId> mysql- connector- java< / artifactId> < version> 5.1 .6 < / 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> < / dependency> < / dependencies>
< / project>
2.UserDao
package com. lq. dao; import com. lq. domain. User;
import org. apache. ibatis. annotations. Select; import java. util. List; public interface UserDao {
@Select ( "select * from user" ) List< User> findAll ( ) ;
}
3.User
package com. lq. domain; import java. io. Serializable;
import java. util. Date; public class User implements Serializable {
private int id; private String username; private Date birthday; private String sex; private String address; public int getId ( ) {
return id; } public void setId ( int id) {
this . id = id; } public String getUsername ( ) {
return username; } public void setUsername ( String username) {
this . username = username; } public Date getBirthday ( ) {
return birthday; } public void setBirthday ( Date birthday) {
this . birthday = birthday; } 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 + '\'' + ", birthday=" + birthday + ", sex='" + sex + '\'' + ", address='" + address + '\'' + '}' ; }
}
4.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 a ConsoleAppender using a PatternLayout.
log4j. appender. CONSOLE= org. apache. log4j. ConsoleAppender
log4j. appender. CONSOLE. layout= org. apache. log4j. PatternLayout
log4j. appender. CONSOLE. layout. ConversionPattern= % d{
ISO8601} % - 6 r [ % 15.15 t] % - 5 p % 30.30 c % x - % m\n# LOGFILE is set to be a File appender using a 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} % - 6 r [ % 15.15 t] % - 5 p % 30.30 c % x - % m\n
5.SqlMapConfig.xml
< ? xml version= "1.0" encoding= "UTF-8" ? >
< ! DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" >
< ! -- mybatis的主配置文件-- >
< 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/eesy" / > < property name= "username" value= "root" / > < property name= "password" value= "root" / > < / dataSource> < / environment> < / environments> < ! -- 注解配置的话,用class 属性指定被注解的dao的全限定类名-- > < mappers> < mapper class = "com.lq.dao.UserDao" / > < / mappers>
< / configuration>
6.MybatisTest
package com. lq. test; import com. lq. dao. UserDao;
import com. lq. 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. IOException;
import java. io. InputStream;
import java. util. List; public class MybatisTest {
public static void main ( String[ ] args) throws IOException {
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 ( ) ; } }