@ Reference article, on this basis slightly modified
1, new Maven project import dependency
<Dependencies> <Dependency> <groupId>Org.mybatis</groupId> <Artifactid>MyBatis</Artifactid> <version>3.2.8</version> </Dependency> <Dependency> <groupId>Com.microsoft.sqlserver</groupId> <Artifactid>Sqljdbc4</Artifactid> <version>4.0</version> </Dependency> </Dependencies>
View Code
New Yanan.java under 2,com.mybatis package
PackageCom.mybatis; Public classYanan {Private intIID; PrivateString Ifullname; PrivateString Iloginname; PrivateString Ipassword; PublicYanan () {} PublicYanan (intID, String name) { This. IID =ID; This. Ifullname =name; } PublicString Getifullname () {returnIfullname; } Public intgetiid () {returnIID; } Public voidSETIID (intiid) { This. IID =IID; } Public voidsetifullname (String ifullname) { This. Ifullname =Ifullname; } PublicString Getiloginname () {returnIloginname; } Public voidsetiloginname (String iloginname) { This. Iloginname =Iloginname; } PublicString Getipassword () {returnIpassword; } Public voidSetipassword (String ipassword) { This. Ipassword =Ipassword; }}
View Code
3, create a new yanan.xml in the root directory
<?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 "> <Mappernamespace= "Yanan"> <SelectID= "Selectuser"ParameterType= "int"Resulttype= "Yanan">SELECT * from Yanan WHERE id = #{id}</Select> <SelectID= "Selectusers"Resulttype= "Yanan">SELECT * from Yanan</Select> </Mapper>
View Code
4, create a new configuration.xml in the root directory
<?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> <typealiases> <Typealiasalias= "Yanan"type= "Com.mybatis.Yanan"></Typealias> </typealiases> <Environmentsdefault= "Development"> <EnvironmentID= "Development"> <TransactionManagertype= "JDBC"></TransactionManager> <DataSourcetype= "Pooled"> < Propertyname= "Driver"value= "Com.microsoft.sqlserver.jdbc.SQLServerDriver" /> < Propertyname= "url"value= "Jdbc:sqlserver://localhost:1433;databasename=test" /> < Propertyname= "username"value= "sa" /> < Propertyname= "Password"value= "Yanan" /> </DataSource> </Environment> </Environments> <mappers> <MapperResource= "Yanan.xml"/> </mappers> </Configuration>
View Code
New test file under 5.com.controller package
PackageCom.controller;Importjava.io.IOException;ImportJava.io.Reader;Importjava.util.List;Importorg.apache.ibatis.io.Resources;Importorg.apache.ibatis.session.SqlSession;Importorg.apache.ibatis.session.SqlSessionFactory;ImportOrg.apache.ibatis.session.SqlSessionFactoryBuilder;ImportCom.mybatis.Yanan; Public classTest { Public Static voidMain (string[] args)throwsIOException {//TODO auto-generated Method StubString resource = "Configuration.xml"; Reader Reader=Resources.getresourceasreader (Resource); Sqlsessionfactory SSF=NewSqlsessionfactorybuilder (). build (reader); Sqlsession Session=ssf.opensession (); Try{Yanan User= Session.selectone ("Selectuser", 1); System.out.println (User.getifullname ()); SYSTEM.OUT.PRINTLN (user); System.out.println ("--------------Divider Line---------------"); List<Yanan> users = session.selectlist ("Selectusers"); for(inti=0; I<users.size (); i++) {System.out.println (Users.get (i). Getifullname ()); } } Catch(Exception e) {e.printstacktrace (); } finally{session.close (); } }}
View Code
Source code package Download: Javamybatis.rar
Project structure:
Java+maven+sqlserver using MyBatis