MyBatis User table Record Count query

Source: Internet
Author: User

build MyBatis development environment, realize user table record number query 1, create project in MyEclipse, import MyBatis jar Package

2. Create MyBatis configuration file mybatis-Config database information
 mybatis-config file

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE configurationpublic "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd" ><!--through this configuration file, the connection between MyBatis and the database is completed. -<Configuration> <!--referencing the properties file - <PropertiesResource= "Jdbc.properties"/> <Environmentsdefault= "Development"> <EnvironmentID= "Development"> <!--Configuring transaction Management with JDBC - <TransactionManagertype= "JDBC"></TransactionManager> <!--pooled:mybatis Data source, JNDI: Tomcat-based data source - <DataSourcetype= "Pooled"> < Propertyname= "Driver"value= "${driver}"/> < Propertyname= "username"value= "${username}"/> < Propertyname= "Password"value= "${password}"/> < Propertyname= "url"value= "${url}"/> </DataSource> </Environment> </Environments></Configuration>
Jdcb.properties file (com.mysql.jdbc.Driver must be capitalized)
driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/testdbusername=  Rootpassword=BDQN

Mybatis-config.xml header file: Go to dbf file start location find

<! DOCTYPE configurationpublic "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd"  >

A, copy the Mybatis-3.2.2\org\apache\ibatis\builder\xml under the 2 files (below) put it where you want (I put the project directly)

        
b, then in Window-->prefenence-->xml-->xml catalog-->add-->config.dtd this file, then key write -//mybatis.org// This file can be used normally after the DTD Config 3.0//en--> is confirmed.
  3. Write entity class user
 PackageCn.bdqn.pojo; Public classUser {PrivateInteger ID; PrivateString username; PrivateString Usercode; PrivateString UserPassword;  PublicInteger getId () {returnID; }     Public voidsetId (Integer id) { This. ID =ID; }     PublicString GetUserName () {returnusername; }     Public voidSetusername (String username) { This. Username =username; }     PublicString Getusercode () {returnUsercode; }     Public voidSetusercode (String usercode) { This. Usercode =Usercode; }     PublicString Getuserpassword () {returnUserPassword; }     Public voidSetUserPassword (String userpassword) { This. UserPassword =UserPassword; }        }

Test class:

 Public classUsertest {PrivateLogger Logger = Logger.getlogger (usertest.class); @Test Public voidcounttest () {String resource= "Mybatis-config.xml"; intCount = 0; Sqlsession sqlsession=NULL; Try {            //1. Read the input stream of the configuration file (Mybatis-config.xml)//Introduction Package, which is Ibatis's packageInputStream is =Resources.getresourceasstream (Resource); System.out.println ("is======" +is ); //2. Create the Sqlsessionfactory object to complete the reading of the configuration fileSqlsessionfactory factory =NewSqlsessionfactorybuilder (). Build (IS); System.out.println ("factory======" +factory); //3. Create a Sqlsession objectSqlsession =factory.opensession (); System.out.println ("sqlsession=====" +sqlsession); System.out.println (Sqlsession.selectone ("Cn.bdqn.dao.UserMapper.count")); //4. Call the mapper file for data operation (the mapper file must be added to the mybatis-config.xml before calling)Count = Sqlsession.selectone ("Cn.bdqn.dao.UserMapper.count"); System.out.println ("count====" +count); //Log with Log4j.propertiesLogger.debug ("Counttest--->" +count); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{sqlsession.close (); } assert.assertequals (6, Count); }

 4. Configure the mapping file Usermapper.xml

A, header files: Search: sqlsession You can find the header file under Explore mapping SQL statements
B, then in Window-->prefenence-->xml-->xml catalog-->add-->mapper.dtd this file, then key write -//mybatis.org This file will be used normally after the DTD Mapper 3.0//en--> is confirmed.
  
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE mapperpublic "-//mybatis.org//dtd Mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd "><!--If I want to call the mapper file, I'll get the name of the Namespace+id (method name) to access -<Mappernamespace= "Cn.bdqn.dao.UserMapper">    <!--ID to be unique, usually the method name of the drop-down class -    <!--what type of int is returned -    <SelectID= "Count"Resulttype= "int">Select COUNT (1) from user</Select>    <!--Increase -    <!--<insert id= "" ></insert> -</Mapper>

MyBatis User table Record Count query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.