1.pom introduced
<dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.17</version> </dependency> </dependencies>
2. config configuration. Add mybatis-config.xml in resource
<?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=""/> <property name="username" value=""/> <property name="password" value=""/> </dataSource> </ Environment> </environments> <by mappers> <-! add a mapping file Mybatis global configuration file -> <Resource Mapper = "Learn / MyBatis / CarrierMapper.xml" /> </ by mappers> </ Configuration>
3. Add the development needs of the mapper, entity classes
CarrierMapper.xml
<? xml Version = "1.0" encoding = "UTF-8"?> <DOCTYPE Mapper the PUBLIC! "- // mybatis.org//DTD Mapper 3.0 // EN" "http://mybatis.org/dtd/mybatis mapper.dtd--3 "> <-! namespace: namespace id: uniquely identifies resultType: return value type # {id}: id taken from the parameter values passed over the -> <Mapper namespace =" learn.mybatis .CarrierMapper "> <SELECT ID =" queryAll "the resultType =" learn.mybatis.Carrier "> SELECT ID, carrier_no carrierNo, CARRIER_NAME CarrierName, main_page MainPage from t_carrier WHERE IS_DELETED = 0 </ SELECT> </ Mapper>
CarrierMapper.java
package learn.mybatis; import java.util.List; /** * Create by tianqing on 2019/7/24. */ public interface CarrierMapper { List<Carrier> queryAll(); }
Carrier.java
public class Carrier { private Long id; private String carrierNo; private String carrierName; private String mainPage; /** * 承运商类型 */ private Integer carrierType; private Integer isDeleted; private String creator; private String editor; private Date createTime; private Date editTime;
getset... }
4. The overall directory structure
5. Run the main method.
package learn.mybatis; import com.google.gson.Gson; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.InputStream; import java.util.List; /** * Create by tianqing on 2019/7/23. */ public class MybatisLearn { private static final Logger logger = LoggerFactory.getLogger(MybatisLearn.class); public static void main(String[] args) throws Exception { //前三步都相同 InputStream = Resources.getResourceAsStream InputStream ( "the mybatis-config.xml"); SqlSessionFactory Factory's new new the SqlSessionFactoryBuilder = () Build (inputStream);. SqlSession SQLSESSION = factory.openSession (); // this is no longer calls the SqlSession api, but get the interface object, interface method calls. Mapper = sqlSession.getMapper CarrierMapper (CarrierMapper.class); List <Carrier> Carriers mapper.queryAll = (); Gson GSON new new Gson = (); logger.info ( "[MybatisLearn - main] Results: {}", gson. the toJson (Carriers)); } }
6. Execute and print.
Run up later. We do not worry multiple uses, mybatis how to achieve the next chapter of research at approximately