SQL--(Spring 和 MyBatis 整合连接数据库 )

Spring 和 MyBatis 整合连接数据库 

方法一:使用 mybatis 配置文件

文件目录:

SQL--(Spring 和 MyBatis 整合连接数据库 )_第1张图片     SQL--(Spring 和 MyBatis 整合连接数据库 )_第2张图片

配置文件:

beans.xml




	 
	 
		
		
			
			
			
			
			
			
			
			
		
			
		
			
			
		
		
		
		
			
			
		
		

jdbc.properties

jdbc.user=root
jdbc.password=******
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/exam

jdbc.initialPoolSize = 5
jdbc.maxPoolSize = 20

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

sqlMapConfig.xml





	


	

UserMapper.xml




	

接口:

UserMapper.java

package com.neuedu.mapper;

import com.neuedu.po.User;

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

文件:User.java

package com.neuedu.po;

public class User {
	private int usersid;
	private String username;
	public int getUsersid() {
		return usersid;
	}
	public void setUsersid(int usersid) {
		this.usersid = usersid;
	}
	public double getAccount() {
		return account;
	}
	public void setAccount(Double account) {
		this.account = account;
	}
	private String password;
	private int phone;
	private String email;
	
	private Double account;
	public int getUserid() {
		return usersid;
	}
	public void setUserid(int usersid) {
		this.usersid = usersid;
	}
	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;
	}
	public Double getcount() {
		return account;
	}
	public void setCount(Double count) {
		this.account = account;
	}
	@Override
	public String toString() {
		return "User [userid=" + usersid + ", username=" + username + ", password=" + password + ", phone=" + phone
				+ ", email=" + email + "]";
	}
	
	
}

UserService.java

package com.neuedu.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neuedu.mapper.UserMapper;
import com.neuedu.po.User;
@Service
public class UserService {
	@Autowired
	private UserMapper userMapper;
	public void getUserById(int usersid) {
	
		User user = (User) userMapper.getUserById(usersid);
		System.out.println(user);
	}
}

测试文件

UserServiceTest.java

package com.neuedu.test;

import static org.junit.Assert.*;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.neuedu.service.UserService;

public class UserServiceTest {

	@Test
	public void test() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
		UserService userService = ac.getBean(UserService.class);
		userService.getUserById(1);
	}

}

输出:

SQL--(Spring 和 MyBatis 整合连接数据库 )_第3张图片

方法二:别名(将所有配置文件写到 beans.xml)

修改代码:

删除:sqlMapConfig.xml

UserMapper.xml




	

beans.xml




	 
	 
		
		
			
			
			
			
			
			
			
			
		
			
		
			
			
         
		
		
		
		
			
			
		
		

配置扫描别名的包

输出:

SQL--(Spring 和 MyBatis 整合连接数据库 )_第4张图片

你可能感兴趣的:(#,Spring,#,数据库,突破Spring框架)