软件框架,通常指的是为了实现某个业界标准或完成特点基本任务的软件组件规范,也指为了实现某个软件组件规范时,提供规范所要求之基础功能的软件产品。
框架是一个半成品,在半成品的基础上完成自己的业务。目前我所学的框架是基于jar包(class文件)以及xml配置文件(调用class文件)
三层架构:dao,service,servlet(git)
1.Mybatis是一个dao层的一个框架,ibatis-->Mybatis
2.Mybatis是一个半自动化的ORM框架
特点:
1.可以简化代码,去除重复繁琐的代码;
2.可以自动完成对象和数据库数据的转换;
3.支持缓存机制以及动态SQL;
4.支持将表与表之间的关系转换为实体与实体之间的关系;
public class User { private int id; private String username; private Date birthday; private String sex; private String address; } 复制代码
<?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.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis01"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <!--配置映射文件 --> <mappers> <mapper resource="com/mybatis01/bean/UserMapper.xml"/> </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="org.mybatis.example.BlogMapper"> <select id="selectUser" resultType="com.mybatis01.bean.User"> select * from user where id = #{id} </select> </mapper> 复制代码
public class UserTest { @Test public void Test01() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); User user = sqlSession.selectOne("selectUser", 1); System.out.println(user); } } 复制代码
# Global logging configuration log4j.rootLogger=DEBUG, stdout # MyBatis logging configuration... log4j.logger.org.mybatis.example.BlogMapper=TRACE # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n 复制代码