当前位置: 代码迷 >> 综合 >> mybatis-introduce-有传统实现类
  详细解决方案

mybatis-introduce-有传统实现类

热度:73   发布时间:2023-11-23 14:25:37.0

在这里插入图片描述

mybatis-introduce-有传统实现类

1.pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.lq</groupId><artifactId>mybatis-introduce</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><dependencies><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.5</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.12</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.10</version></dependency></dependencies>
</project>

2.UserDaoImpl

package com.lq.dao.impl;import com.lq.dao.UserDao;
import com.lq.domain.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;import java.util.List;public class UserDaoImpl implements UserDao {
    private SqlSessionFactory factory;public UserDaoImpl(SqlSessionFactory factory){
    this.factory = factory;}public List<User> findAll() {
    //1.使用工厂创建SqlSession对象SqlSession session = factory.openSession();//2.使用session执行查询所有方法List<User> users = session.selectList("com.lq.dao.UserDao.findAll");session.close();//3.返回查询结果return users;}
}

3.UserDao

package com.lq.dao;import com.lq.domain.User;
import org.apache.ibatis.annotations.Select;import java.util.List;public interface UserDao {
    List<User> findAll();
}

4.User

package com.lq.domain;import java.io.Serializable;
import java.util.Date;public class User implements Serializable {
    private int id;private String username;private Date birthday;private String sex;private String address;public int getId() {
    return id;}public void setId(int id) {
    this.id = id;}public String getUsername() {
    return username;}public void setUsername(String username) {
    this.username = username;}public Date getBirthday() {
    return birthday;}public void setBirthday(Date birthday) {
    this.birthday = birthday;}public String getSex() {
    return sex;}public void setSex(String sex) {
    this.sex = sex;}public String getAddress() {
    return address;}public void setAddress(String address) {
    this.address = address;}@Overridepublic String toString() {
    return "User{" +"id=" + id +", username='" + username + '\'' +", birthday=" + birthday +", sex='" + sex + '\'' +", address='" + address + '\'' +'}';}
}

5.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# 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

6.SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--mybatis的主配置文件-->
<configuration><!--配置环境--><environments default="mysql"><environment id="mysql"><transactionManager type="JDBC"></transactionManager><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/eesy"/><property name="username" value="root"/><property name="password" value="root"/></dataSource></environment></environments><!--指定映射配置文件的位置,映射配置文件指的是每个dao独立的配置文件--><mappers><mapper resource="com/lq/dao/UserDao.xml"/></mappers>
</configuration>

7.MybatisTest

package com.lq.test;import com.lq.dao.UserDao;
import com.lq.dao.impl.UserDaoImpl;
import com.lq.domain.User;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;public class MybatisTest {
    public static void main(String[] args) throws IOException {
    //1.读配置文件InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");//2创建SqlSessionFactory工厂SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();SqlSessionFactory factory = builder.build(in);//3使用工厂创建dao对象UserDao userDao = new UserDaoImpl(factory);//4使用代理对象执行方法List<User> users = userDao.findAll();for (User user : users) {
    System.out.println(user);}in.close();}}

8.UserDao.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lq.dao.UserDao"><select id="findAll" resultType="com.lq.domain.User">select * from user</select>
</mapper>
  相关解决方案