SQL--(MyBatis 实战)

MyBatis 实战

数据库

SQL--(MyBatis 实战)_第1张图片

文件目录:

SQL--(MyBatis 实战)_第2张图片     SQL--(MyBatis 实战)_第3张图片

代码:User.xml(配置文件)




	
	

sqlMapConfig.xml(配置文件)



	
	
	
		
		
			
		
			
				
				
				
				
			
		
	
	
	
		 
		

User.java

package com.neuedu.model;

public class User {
	private int usersid;
	private String username;
	private String password;
	private int phone;
	private String email;
	public int getUserid() {
		return usersid;
	}
	public void setUserid(int userid) {
		this.usersid = userid;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getPhone() {
		return phone;
	}
	public void setPhone(int phone) {
		this.phone = phone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	@Override
	public String toString() {
		return "User [usersid=" + usersid + ", username=" + username + ", password=" + password + ", phone=" + phone
				+ ", email=" + email + "]";
	}
	
	
}

UserTest.java

package com.neuedu.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

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.neuedu.model.User;
public class UserTest {
	public static void main(String[] args) throws IOException {
		InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
		SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(inputStream);
		
		SqlSession session = ssf.openSession();
	/*	User user =(User)session.selectOne("test.getUserById",1);
		System.out.println(user);*/
		
		List users = session.selectList("test.getAllUsers");
		for(User user : users) {
			System.out.println(user);
			
		}
		
	}
}

jdbc.properties

jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=oaec
jdbc.password=oaec

log4j.properties

# Global logging configuration
#\u5728\u5f00\u53d1\u73af\u5883\u4e0b\u65e5\u5fd7\u7ea7\u522b\u8981\u8bbe\u7f6e\u6210DEBUG\uff0c\u751f\u4ea7\u73af\u5883\u8bbe\u7f6e\u6210info\u6216error
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

输出:

SQL--(MyBatis 实战)_第4张图片

二、mybatis_Mapper代理方法

文件目录:

SQL--(MyBatis 实战)_第5张图片      SQL--(MyBatis 实战)_第6张图片

代码:User.xml(配置文件)




	
	

sqlMapConfig.xml(配置文件)




	
	
		
		
			
		
			
				
				
				
				
			
		
	
	
	
		 
		

User.java

package com.neuedu.model;

public class User {
	private int usersid;
	private String username;
	private String password;
	private int phone;
	private String email;
	public int getUserid() {
		return usersid;
	}
	public void setUserid(int userid) {
		this.usersid = userid;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getPhone() {
		return phone;
	}
	public void setPhone(int phone) {
		this.phone = phone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	@Override
	public String toString() {
		return "User [usersid=" + usersid + ", username=" + username + ", password=" + password + ", phone=" + phone
				+ ", email=" + email + "]";
	}
	
	
}

UserMapper.java

package com.neuedu.mapper;

import java.util.List;

import com.neuedu.model.User;

public interface UserMapper {
	public User getUserById(int usersid);
	public List getAllUsers();
}

UserMapperTest.java

package com.neuedu.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

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.neuedu.mapper.UserMapper;
import com.neuedu.model.User;

public class UserMapperTest {

	public static void main(String[] args) throws IOException {
		InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
		SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session = ssf.openSession();
		
		UserMapper mapper = session.getMapper(UserMapper.class);
		/*User user = mapper.getUserById(1);
		System.out.println(user);*/
		
		List users = mapper.getAllUsers();
		for(User user : users) {
			System.out.println(user);
			
		}
		                                                                                                                                                                                                                       
		
	}

}

UserTest.java

package com.neuedu.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

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.neuedu.model.User;
public class UserTest {
	public static void main(String[] args) throws IOException {
		InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
		SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(inputStream);
		
		SqlSession session = ssf.openSession();
	/*	User user =(User)session.selectOne("test.getUserById",1);
		System.out.println(user);*/
		
		List users = session.selectList("test.getAllUsers");
		for(User user : users) {
			System.out.println(user);
			
		}
		
	}
}

输出:

SQL--(MyBatis 实战)_第7张图片

 

你可能感兴趣的:(#,数据库)