javaee实现创建Company类和Employee类,在Company类中添加Employee属性和name属性,在Employee类中添加name属性,在数据库创建Company表和Employee表,通过MyBatis查询id为1的Company类的信息(Company类中需要含有所属的Employee类的列表)。
时间: 2024-02-16 19:00:00 浏览: 141
好的,我会尽力回答你的问题。
首先,我们来创建Java类和数据库表。
1. Company类
```java
public class Company {
private int id;
private String name;
private List<Employee> employees;
// getter and setter methods
}
```
2. Employee类
```java
public class Employee {
private int id;
private String name;
// getter and setter methods
}
```
3. 创建数据库表
```sql
CREATE TABLE company (
id INT PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE employee (
id INT PRIMARY KEY,
name VARCHAR(50),
company_id INT,
FOREIGN KEY (company_id) REFERENCES company(id)
);
```
接下来,我们使用MyBatis进行数据库操作。
1. 在pom.xml文件中添加MyBatis和数据库驱动的依赖:
```xml
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
</dependencies>
```
2. 在MyBatis的配置文件中配置数据库连接和映射文件:
```xml
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/mapper/CompanyMapper.xml"/>
</mappers>
</configuration>
```
3. 创建CompanyMapper.xml文件,定义查询语句:
```xml
<mapper namespace="com.example.mapper.CompanyMapper">
<resultMap id="companyResultMap" type="com.example.model.Company">
<id column="id" property="id"/>
<result column="name" property="name"/>
<collection property="employees" ofType="com.example.model.Employee">
<id column="employee_id" property="id"/>
<result column="employee_name" property="name"/>
</collection>
</resultMap>
<select id="getCompanyById" resultMap="companyResultMap">
SELECT c.id, c.name, e.id AS employee_id, e.name AS employee_name
FROM company c
LEFT JOIN employee e ON c.id = e.company_id
WHERE c.id = #{id}
</select>
</mapper>
```
4. 编写Java代码,调用MyBatis查询数据:
```java
public class CompanyService {
private SqlSessionFactory sqlSessionFactory;
public CompanyService() {
try {
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public Company getCompanyById(int id) {
try (SqlSession session = sqlSessionFactory.openSession()) {
CompanyMapper mapper = session.getMapper(CompanyMapper.class);
return mapper.getCompanyById(id);
}
}
}
```
最后,在Main方法中调用getCompanyById方法:
```java
public class Main {
public static void main(String[] args) {
CompanyService companyService = new CompanyService();
Company company = companyService.getCompanyById(1);
System.out.println(company.getId() + " " + company.getName());
for (Employee employee : company.getEmployees()) {
System.out.println(employee.getId() + " " + employee.getName());
}
}
}
```
这样,就可以查询id为1的Company类的信息,并且Company类中含有所属的Employee类的列表了。
阅读全文