MyBatis


log4j异常
创建log4j.properties文件
MyBatis

 

 


log4j.rootLogger=INFO,console,dailyFile # TODO 发布到阿里云记得添加,另外控制台不输出(只输出warn或者error信息) log4j.logger.org.mybatis = info log4j.logger.com.zmj.mapper=debug log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.encoding=UTF-8 log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%l] - [%p] %m%n # 定期滚动日志文件,每天都会生成日志 log4j.appender.dailyFile=org.apache.log4j.DailyRollingFileAppender log4j.appender.dailyFile.encoding=UTF-8 log4j.appender.dailyFile.Threshold=INFO # TODO 本地日志地址,正式环境请务必切换为阿里云地址 log4j.appender.dailyFile.File=C:/logs/maven-ssm-alipay/log.log4j log4j.appender.dailyFile.DatePattern='.'yyyy-MM-dd log4j.appender.dailyFile.layout=org.apache.log4j.PatternLayout log4j.appender.dailyFile.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%l] - [%p] %m%n

 pom.xml

    <groupId>org.example</groupId>
    <artifactId>mybatis</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory><!--所在的目录-->
                <includes><!--包括目录下的.properties,.xml 文件都会扫描到-->
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

 

userDao
public interface userDao {
    public List<user> selectuser();
}

userDao.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="mybatis.Dao.userDao">
    <select id="selectuser" resultType="com.gg.domain.user">
    select * from user
  </select>
</mapper>

 

mybatis.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>

    <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://127.0.0.1:3306/students?characterEncoding=utf8&amp;useSSL=true&amp;serverTimezone=UTC&amp;rewriteBatchedStatements=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="820239186"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/gg/Dao/userDao.xml"/>
    </mappers>

</configuration>

Myapp 测试

    public static void main(String[] args) throws IOException {
        String resource = "mybatis.xml";
        Reader reader = Resources.getResourceAsReader(resource);
        SqlSessionFactoryBuilder builder =new SqlSessionFactoryBuilder();
        SqlSessionFactory factory =builder.build(reader);
        SqlSession sqlSession =factory.openSession();
        String sqlId="mybatis.Dao.userDao" + "." +"selectuser";
        List<user> userList= sqlSession.selectList(sqlId);
        userList.forEach(user-> System.out.println(userList));
        sqlSession.close();
    }

 






























上一篇:配置文件模板


下一篇:log4j