标签:mybatis框架 debug version jdbc otc create 框架 pass man
mybatis框架是实现与数据之间交互
入门:
创建数据库环境
创建实体类与数据库对应字段 实现Serializable
创建接口定义方法
创建主配置方法:
<?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="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/javaee"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <!-- 指定映射文件--> <mappers> <mapper resource="cn/dao/UserDao.xml"></mapper> </mappers> </configuration>
创建映射文件;
<?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="cn.dao.UserDao"> <select id="findAll" resultType="cn.mepu.User"> select * from user </select> </mapper>
测试类编写;
package cn.mepu; import cn.dao.UserDao; 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; /** * @author shkstart * @create 2019-11-01 10:35 */ public class UserTest { public static void main(String[] args) throws IOException { // 1.读取配置文件 InputStream in = Resources.getResourceAsStream("SqlMapConifg.xml"); // 2.创建工厂SqlSessionFactory SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(in); // 3.使用工厂获得SqlSession对象 SqlSession session = factory.openSession(); // 4.使用SqlSession对象创建Dao接口代理对象 UserDao userDao = session.getMapper(UserDao.class); // 5.使用代理对象执行方法 List<User> users = userDao.findAll(); for (User user : users) { System.out.println(user); } // 6.释放资源 session.close(); in.close(); } }
导入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\nc
标签:mybatis框架 debug version jdbc otc create 框架 pass man
原文地址:https://www.cnblogs.com/aikang525/p/11776061.html