您的位置:首页 > 移动开发

mybatis——使用mapper代理开发方式

2015-07-21 16:39 543 查看
---------------------------------------------------------------generatorConfig.xml-------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >

<generatorConfiguration>

<!--加载属性文件 -->

<properties resource="db.properties" />

<context id="context1">

<commentGenerator>

<property name="suppressDate" value="true" />

<!-- 是否去除自动生成的注释 true:是 : false:否 -->

<property name="suppressAllComments" value="true" />

</commentGenerator>

<!-- 数据库连接URL,用户名,密码 -->

<jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}" />

<!--生成模型的包名和位置 -->

<javaModelGenerator targetPackage="mode" targetProject="demo2Mybatis/src" />

<!--映射文件的包名和位置 -->

<sqlMapGenerator targetPackage="mapper" targetProject="demo2Mybatis/src" />

<!--DAO的包名和位置 -->

<javaClientGenerator targetPackage="mapper" targetProject="demo2Mybatis/src" type="XMLMAPPER" />

<!--要生成哪些表 -->

<table schema="" tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false"

enableDeleteByExample="false" enableSelectByExample="false" enableInsert="false" />

<table schema="" tableName="address" domainObjectName="Address" enableCountByExample="false" enableUpdateByExample="false"

enableDeleteByExample="false" enableSelectByExample="false" enableInsert="false" />

</context>

</generatorConfiguration>

---------------------------------------------------------------------log4j.properties------------------------------------------------------------------------------

log4j.rootLogger=DEBUG, stdout

# 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

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

<!-- 加载属性文件 -->

<properties resource="db.properties">

<!--properties中还可以配置一些属性名和属性值 -->

<!-- <property name="jdbc.driver" value=""/> -->

</properties>

<!-- 全局配置参数,需要时再设置 -->

<!-- <settings>

</settings> -->

<!-- 别名定义 -->

<typeAliases>

<!-- 针对单个别名定义

type:类型的路径

alias:别名

-->

<!-- <typeAlias type="cn.itcast.mybatis.po.User" alias="user"/> -->

<!-- 批量别名定义

指定包名,mybatis自动扫描包中的po类,自动定义别名,别名就是类名(首字母大写或小写都可以)

-->

<package name="mode"/>

</typeAliases>

<!-- 和spring整合后 environments配置将废除-->

<environments default="development">

<environment id="development">

<!-- 使用jdbc事务管理,事务控制由mybatis-->

<transactionManager type="JDBC" />

<!-- 数据库连接池,由mybatis管理-->

<dataSource type="POOLED">

<property name="driver" value="${jdbc.driver}" />

<property name="url" value="${jdbc.url}" />

<property name="username" value="${jdbc.username}" />

<property name="password" value="${jdbc.password}" />

</dataSource>

</environment>

</environments>

<!-- 通过mapper接口加载映射文件:需要遵循一些规范:需要将mapper接口名称和mapper.xml映射文件保持一致,且在一个目录中

放在一个目录 ,且同名

前提是:使用的事mapper代理方式 -->

<mappers>

<package name="mapper" />

</mappers>

</configuration>

-----------------------------------------------------------Demo1.java-------------------------------------------------------------------------------------------

public class Demo1 {

private SqlSessionFactory sqlSessionFactory;

// 此方法是在执行testFindUserById之前执行

@Before

public void setUp() throws Exception {

// 创建sqlSessionFactory

// mybatis配置文件

String resource = "SqlMapConfig.xml";

// 得到配置文件流

InputStream inputStream = Resources.getResourceAsStream(resource);

// 创建会话工厂,传入mybatis的配置文件信息

sqlSessionFactory = new SqlSessionFactoryBuilder()

.build(inputStream);

}

//用户信息的综合 查询

@Test

public void testFindUserList() throws Exception {

SqlSession sqlSession = sqlSessionFactory.openSession();

//创建UserMapper对象,mybatis自动生成mapper代理对象

UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

User user = userMapper.selectByPrimaryKey(6);

System.out.println(user);

}

}

------------------------------------------------------------------------------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: