<?xml version="1.0" encoding="UTF-8" ?>java
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >spring
<generatorConfiguration>sql
<!--加載屬性文件 -->數據庫
<properties resource="db.properties" />apache
<context id="context1">mybatis
<commentGenerator>app
<property name="suppressDate" value="true" />dom
<!-- 是否去除自動生成的註釋 true:是 : false:否 -->ui
<property name="suppressAllComments" value="true" />url
</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.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
<?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);
}
}