mybatis详解(四)

spring与mybatis的集成

1,新建maven的quickstart项目

2,导入需要的坐标,pom.xml

4.0.0com.shsxtspring_mybatis1.0-SNAPSHOTspring_mybatishttp://www.example.comUTF-81.81.8junitjunit4.12testorg.springframeworkspring-context4.3.2.RELEASEorg.springframeworkspring-test4.3.2.RELEASEorg.springframeworkspring-jdbc4.3.2.RELEASEorg.springframeworkspring-tx4.3.2.RELEASEorg.aspectjaspectjweaver1.8.9c3p0c3p00.9.1.2org.mybatismybatis3.4.1org.mybatismybatis-spring1.3.0mysqlmysql-connector-java5.1.39org.slf4jslf4j-log4j121.7.2org.slf4jslf4j-api1.7.2com.github.pagehelperpagehelper4.1.0org.mybatis.generatormybatis-generator-maven-plugin1.3.2src/main/resources/generatorConfig.xmltruetruespring_mybatissrc/main/resourcessrc/main/java**/*.properties**/*.xml**/*.tldfalse

3,创建resources文件夹,在该文件下创建log4j.properties和db.properties,mybatis.xml,spring.xml

log4j.properties

# Global logging configurationlog4j.rootLogger=DEBUG, stdout# Console output...log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

db.properties

driver=com.mysql.jdbc.

Driverurl=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&useSSL=falsename=rootpassword=123456

mybatis.xml

countsql"/>

spring.xml

        http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context.xsd

        http://www.springframework.org/schema/aop

        http://www.springframework.org/schema/aop/spring-aop.xsd

        http://www.springframework.org/schema/tx

        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

4,创建包,目录结构如下,暂且不需要创建包下的文件,后面会自动生成,


因为我们在pom.xml中导入了代码自动生成的插件,现在需要在resources下添加generatorConfig.xml

需要修改几个地方即可,包名需要也需要事先创建好,如果需要同时生成多个实体类可以有多个table标签



双击如下图所示的位置,即可生成文件


会自动生成如下文件


创建Userservice类

package com.shsxt.services;importcom.github.pagehelper.PageHelper;importcom.github.pagehelper.PageInfo;importcom.shsxt.dao.UserDao;importcom.shsxt.po.User;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Repository;importjava.util.List;@RepositorypublicclassUserService{        @AutowiredprivateUserDaouserDao;publicUserqueryById(Integerid){returnuserDao.queryUserById(id);        }publicPageInfo queryUserByparams(IntegerpageNum,IntegerpageSize,Stringusername){PageHelper.startPage(pageNum, pageSize);List list=userDao.queryUserByparams(username);PageInfo pageInfo=newPageInfo(list);returnpageInfo;        }}

测试

importcom.github.pagehelper.PageInfo;importcom.shsxt.po.User;importcom.shsxt.services.UserService;importorg.apache.ibatis.reflection.SystemMetaObject;importorg.junit.runner.RunWith;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.test.context.ContextConfiguration;importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations={"classpath:spring.xml"} )publicclassTest{    @AutowiredprivateUserServiceuserService;    @org.junit.Testpublicvoid test01(){System.out.println(userService.queryById(1));    }}

测试结果成功


你可能感兴趣的:(mybatis详解(四))