Ah, I'm a beginner in idea, Maven, and mybatis. It's a mess of pain...
1. Use idea to create a web project
2. Enter groupid and artifactid, and select archetype: Web
3. create SRC \ main \ Java, Src \ test \ Java, Src \ test \ resources, and create several packages: COM. mymaven. mybatisdemo2, Com. mymaven. mybatisdemo2.po, Com. mymaven. mybatisdemo2.po. the directory structure of Dao is as follows:
4. Edit Pom. xml and add dependency
<Dependency> <groupid> JUnit </groupid> <artifactid> JUnit </artifactid> <version> 4.9 </version> </dependency> <! -- Mybatis --> <dependency> <groupid> Org. mybatis </groupid> <artifactid> mybatis </artifactid> <version> 3.2.2 </version> </dependency> <groupid> Org. mybatis </groupid> <artifactid> mybatis-spring </artifactid> <version> 1.1.1 </version> </dependency> <! -- Data source --> <dependency> <groupid> COM. microsoft. jdbc </groupid> <artifactid> COM. microsoft. jdbc </artifactid> <version> jdbc4 </version> </dependency> <groupid> commons-DBCP </groupid> <artifactid> commons-DBCP </artifactid> <version> 1.4 </version> </dependency> <groupid> commons-pool </groupid> <artifactid> commons-pool </artifactid> <version> 1.6 </version> </dependency> <! -- Spring --> <dependency> <groupid> Org. springframework </groupid> <artifactid> spring-core </artifactid> <version> 3.2.3.release </version> </dependency> <groupid> Org. springframework </groupid> <artifactid> spring-beans </artifactid> <version> 3.2.3.release </version> </dependency> <groupid> Org. springframework </groupid> <artifactid> spring-context </artifactid> <version> 3.2.3.release </version> </dependency>
After saving the file, click the maven project button ---- lifecycle ----> clean -----> package ----> refresh button to automatically download the dependency.
3. Java class
Department under com. mymaven. mybatisdemo2.po
Package COM. mymaven. mybatisdemo2.po; public class Department {private string dp_id; private string dp_name; private string cost_center; Public String getdp_id () {return dp_id;} public void setdp_id (string dp_id) {This. dp_id = dp_id;} Public String getdp_name () {return dp_name;} public void setdp_name (string dp_name) {This. dp_name = dp_name;} Public String getcost_center () {return cost_center;} public void s Etcost_center (string cost_center) {This. cost_center = cost_center;}/* No parameter constructor, required! */Public department (){}}
The departmentmapper under com. mymaven. mybatisdemo2.dao is just an interface and does not need to write impl classes.
package com.mymaven.mybatisdemo.dao;import com.mymaven.mybatisdemo.po.Department;public interface DepartmentMapper {public void addDepartMent(Department dpt);public Department queryByDpName(String dp_name);public void updateDepartment(Department dpt);}
4. Add the configuration file. Put the mybatis-config.xml and applicationcontext. xml under the SRC \ main \ Resources Directory; put the Mapper class configuration file under the SRC \ main \ resources \ mybatis \ directory
:
Applicationcontext. xml
<? XML version = "1.0" encoding = "UTF-8"?> <BeansDefault-autowire = "bytype"Xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: P = "http://www.springframework.org/schema/p" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <! -- Data source <bean id = "datasource" class = "com. microsoft. sqlserver. JDBC. sqlserverdriver "> <property name =" url "value =" JDBC: sqlserver: // localhost: 1433; databasename = ots_vacation "/> <property name =" username "value =" sa "/> <property name =" password "value =" 1866276620.whf "/> <property name = "" </bean> --> <bean id = "datasource" class = "org. apache. commons. DBCP. basicdatasource "Destroy-method =" close "> <property name = "Driverclassname" value = "com. microsoft. sqlserver. JDBC. sqlserverdriver "/> <property name =" url "value =" JDBC: sqlserver :/// localhost: 1433; databasename = ots_vacation "/> <property name =" username "value =" sa "/> <property name =" password "value =" 123456 "/> </bean> <! -- Sqlsessionfactory --> <bean id = "sqlsessionfactory" class = "org. mybatis. Spring. sqlsessionfactorybean"> <property name = "datasource" ref = "datasource"/>
<! -- The configuration file path of mybatis-config and mapper is specified here --> <property name = "configlocation" value = "classpath: mybatis-config.xml "/> <property name =" mapperlocations "value =" classpath *: mybatis/mapper /*. XML "/> </bean> <! -- Registration Method 1: Specify mapper <bean id = "groupmapper" class = "org. mybatis. spring. mapper. mapperfactorybean "> <property name =" mapperinterface "value =" com. mymaven. mybatisdemo. dao. groupdao "/> <property name =" sqlsessionfactory "ref =" sqlsessionfactory "/> </bean> <bean id =" departmentmapper "class =" org. mybatis. spring. mapper. mapperfactorybean "> <property name =" mapperinterface "value =" com. mymaven. mybatisdemo. dao. departmen Tmapper "/> <property name =" sqlsessionfactory "ref =" sqlsessionfactory "/> </bean> --> <! -- Mapper Registration Method 2: do not specify a specific Mapper, but use the automatic scan package method to register various Mapper. The configuration is as follows: --> <Bean class = "org. mybatis. spring. mapper. mapperscannerconfigurer "> <property name =" basepackage "value =" com. mymaven. mybatisdemo. dao "/> </bean> </beans>
Mybatis-config.xml, which primarily uses its setting part and typealiases
<? 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> <Settings> <! -- Changes from the defaults for testing --> <setting name = "cacheenabled" value = "false"/> <setting name = "usegeneratedkeys" value = "true"/> <setting name = "defaultexecutortype" value = "reuse"/> </Settings> <! -- If this parameter is not specified, mapper. department cannot be used for the resulttype or parametertype of XML, But Com. mymaven. mybatisdemo. po. department --> <typealiases> <typealias type = "com. mymaven. mybatisdemo. po. department "alias =" department "/> <typealias type =" com. mymaven. mybatisdemo. po. group "alias =" group "/> </typealiases> </configuration>
Departmentmapper. xml
<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype mapper public "-// mybatis.org//dtd mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <! -- In this namespace, you must specify the DaO interface --> <mapper namespace = "com. mymaven. mybatisdemo. Dao. departmentmapper"> <insert id = "adddepartment" parametertype = "department"> <! -- The SQL statement cannot end with a semicolon. The field name corresponds to the pojo class --> insert into t_department values (# {dpid}, # {dpname}, # {costcenter }) </Insert> <select id = "querybydpname" resulttype = "department" parametertype = "Java. lang. string "> select * From t_department where dp_name =#{ value} </SELECT> <update id =" updatedepartment "parametertype =" department "> Update t_department set dp_name =#{ dpname}, cost_center =#{ costcenter} Where dp_id =#{ dp_id} </update> </mapper>
Test code:
Package COM. mymaven. mybatisdemo. test; import COM. mymaven. mybatisdemo. dao. departmentmapper; import COM. mymaven. mybatisdemo. dao. groupdao; import COM. mymaven. mybatisdemo. po. group; import COM. mymaven. mybatisdemo. utils. mybatishelper; import COM. mymaven. mybatisdemo. po. department; import Org. apache. ibatis. session. sqlsession; import Org. apache. ibatis. session. sqlsessionfactory; import Org. springframework. context. AP Plicationcontext; import Org. springframework. context. support. filesystemxmlapplicationcontext;/*** created with intellij idea. * User: weihaifeng * Date: 13-6-8 * Time: 11 am * to change this template use file | Settings | file templates. */public class testmyabtisdemo {public static void main (string [] ARGs) {testmyabtisdemo tmbd = new testmyabtisdemo (); // tmbd. testadd (); // tmbd. testgroupget (); tmbd. Testget ();} public void testgroupget () {applicationcontext act = new filesystemxmlapplicationcontext ("classpath *: applicationcontext. XML "); groupdao = (groupdao) Act. getbean ("groupmapper"); group Group = groupdao. getgroupbyname ("user"); // system. out. println ("groupid:" + group. getgroup_id () + "\ n groupname:" + group. getgroup_name (); system. out. println (group. tostring ();}/* Public void testadd (){ Try {applicationcontext act = new filesystemxmlapplicationcontext ("classpath *: applicationcontext. XML "); departmentmapper dm = act. getbean (departmentmapper. class); Department DPT = new Department (); DPT. setdpid (math. random () + "11111111"); DPT. setdpname ("mybatis_demo1"); DPT. setcostcenter ("mbatis3"); DM. adddepartment (DPT); system. out. println ("data inserted successfully! ");} Catch (exception e) {e. printstacktrace () ;}} */Public void testget () {try {applicationcontext act = new filesystemxmlapplicationcontext ("classpath *: applicationcontext. XML "); departmentmapper dm = act. getbean (departmentmapper. class); Department DPT = DM. querybydpname ("Department 2"); // system. out. println ("department ID:" + DPT. getdpid () + ", Department name:" + DPT. getdpname () // + ", cost center:" + DPT. getcostcenter (); system. out. println (DPT. tostring ();} catch (exception e) {e. printstacktrace (); // to change body of catch statement use file | Settings | file templates .}}}
Note:
1. the attribute name of the Po class must be the same as that of the database field or query statement! Suppose we have a table t_department ---> corresponding to the pojo class department. If the table field is dp_id and you do not use an alias in the query, the Department class attribute name must be: dp_id. If you set it to dpid or another one, either the query fails during the select operation or other problems
2. For the er registration method, if mapperscannerconfigurer is used, the auto-Wire Automatic Loading Method of spring must be set to bytype! Otherwise, the Business iservices will not be able to automatically inject Dao instances.
Attached: Explanation of auto-wire:
Autowire = "byname" is set in beans, so that the configuration file will automatically find the bean id = "XXX" based on setxxx, and pay attention to the Case sensitivity, and then configure it automatically. If it is not found, it will not be assembled.
If autowire = "byname" is set in beans, the implementation class of the dependent interface is automatically assembled. If it is not found, it is not assembled. If two beans in the same configuration file implement the source interface, an error is returned.