一个账户对应一个用户
accout
CREATE TABLE `account` (
`aid` int(11) NOT NULL AUTO_INCREMENT,
`accountname` varchar(20) DEFAULT NULL,
`money` float(10,2) DEFAULT NULL,
`userId` int(11) DEFAULT NULL,
PRIMARY KEY (`aid`),
KEY `userId` (`userId`),
CONSTRAINT `account_ibfk_1` FOREIGN KEY (`userId`) REFERENCES `user` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
user
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) DEFAULT NULL,
`PASSWORD` varchar(20) DEFAULT NULL,
`sex` varchar(2) DEFAULT NULL,
`birthday` date DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=47 DEFAULT CHARSET=utf8;
Account
package com.william.domain;
/**
* @author :lijunxuan
* @date :Created in 2020/2/2 20:35
* @description :
* @version: 1.0
*/
public class Account {
Integer aid;
String accountname;
Float money;
Integer userId;
User user;
@Override
public String toString() {
return "Account{" +
"aid=" + aid +
", accountname='" + accountname + '\'' +
", money=" + money +
", userId=" + userId +
", user=" + user +
'}';
}
public Integer getAid() {
return aid;
}
public void setAid(Integer aid) {
this.aid = aid;
}
public String getAccountname() {
return accountname;
}
public void setAccountname(String accountname) {
this.accountname = accountname;
}
public Float getMoney() {
return money;
}
public void setMoney(Float money) {
this.money = money;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
User
package com.william.domain;
/**
* @author :lijunxuan
* @date :Created in 2020/2/2 20:37
* @description :
* @version: 1.0
*/
public class User {
Integer id;
String username;
String PASSWORD;
String sex;
String birthday;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", PASSWORD='" + PASSWORD + '\'' +
", sex='" + sex + '\'' +
", birthday='" + birthday + '\'' +
'}';
}
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 String getPASSWORD() {
return PASSWORD;
}
public void setPASSWORD(String PASSWORD) {
this.PASSWORD = PASSWORD;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
package com.william.mapper;
import com.william.domain.Account;
import com.william.domain.QueryVo;
import com.william.domain.Usert;
import org.apache.ibatis.annotations.Param;
import java.util.HashMap;
import java.util.List;
public interface Usermapper {
List findByhaha();
}
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/web02
jdbc.user=root
jdbc.password=root
# 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, info
# 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} %-6r [%15.15t] %-5p %30.30c %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} %-6r [%15.15t] %-5p %30.30c %x - %m\n
package com.william.test;
import com.william.domain.Account;
import com.william.domain.QueryVo;
import com.william.domain.Usert;
import com.william.mapper.Usermapper;
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 org.junit.Test;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
/**
* @author :lijunxuan
* @date :Created in 2020/2/2 13:28
* @description :
* @version: 1.0
*/
public class TestMyBatis {
/**
* 查询所有的账户相同的
*/
@Test
public void selectAll(){
try {
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("Mybatis-config.xml"));
SqlSession sqlSession = sqlSessionFactory.openSession(true);
Usermapper mapper = sqlSession.getMapper(Usermapper.class);
List list = mapper.findByhaha();
for (Account account : list) {
System.out.println(account.getAid());
System.out.println(account);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}