目录结构
创建数据库相应的表
一个账户对应一个用户
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;
domain 实体类
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;
}
}
mapper Dao
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<Account> findByhaha();
}
mapper.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="com.william.mapper.Usermapper">
<resultMap id="accoutMap" type="account">
<id property="aid" column="aid"></id>
<result property="accountname" column="accountname"></result>
<result property="money" column="money"></result>
<result property="userId" column="userId"></result>
<result property="user.id" column="id"></result>
<result property="user.username" column="username"></result>
<result property="user.PASSWORD" column="PASSWORD"></result>
<result property="user.sex" column="sex"></result>
<result property="user.birthday" column="birthday"></result>
</resultMap>
<select id="findByhaha" resultMap="accoutMap">
select * from account a ,user u where a.userId=u.id;
</select>
</mapper>
配置文件
1.db.properties 数据库配置文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/web02
jdbc.user=root
jdbc.password=root
2.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, 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
3.核心配置文件Mybatis-config.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>
<properties resource="db.properties"></properties>
<typeAliases>
<package name="com.william.domain"></package>
</typeAliases>
<environments default="develoment">
<environment id="develoment">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</dataSource>
</environment>
</environments>
<mappers>
<!--<mapper resource="com/william/Mapper.xml"></mapper>-->
<package name="com.william.mapper"></package>
</mappers>
</configuration>
test测试类
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<Account> list = mapper.findByhaha();
for (Account account : list) {
System.out.println(account.getAid());
System.out.println(account);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
测试结果
来源:CSDN
作者:L-李俊漩
链接:https://blog.csdn.net/sinat_39179993/article/details/104149485