Use of Eclipse Maven MyBatis

Source: Internet
Author: User

There are too many tutorials on the use of MAVEN online, which is not covered here. This article is only used to document the configuration and considerations for creating a program with MyBatis in Eclipse using MAVEN.

Create a MAVEN project with eclipse
    • After creating the directory structure as above, this is not built in the Web project using the Maven QuickStart template

    • Add MyBatis and MySQL dependencies to the Pom.xml file after creation

Use MySQL to create a test database and create a user table

CREATE TABLE User (
UserName varchar (primary) key,
USERPWD varchar () NOT NULL,
Usermail varchar (50),
UserAddress varchar (50)
);

    • Create a new user class that corresponds to each field in the user table

The code is as follows

Package Com.ws.dao; Public classUser {PrivateString UserName;PrivateString userpwd;PrivateString Usermail;PrivateString useraddress; Public User(String username,string userpwd,string usermail,string useraddress) { This.UserName=username; This.userpwd=userpwd; This.Usermail=usermail; This.useraddress=useraddress; } Public User(){            } PublicStringGetUserName() {returnUserName; } Public void Setusername(String userName)    {UserName = UserName; } PublicStringgetuserpwd() {returnUserpwd; } Public void setuserpwd(String userpwd)    {userpwd = userpwd; } PublicStringGetusermail() {returnUsermail; } Public void Setusermail(String Usermail)    {usermail = Usermail; } PublicStringgetuseraddress() {returnuseraddress; } Public void setuseraddress(String useraddress)    {useraddress = useraddress; } PublicStringtoString(){return GetUserName()+" "+getuserpwd()+" "+Getusermail()+" "+getuseraddress(); }}
    • Create a new Usermapper interface with the following code
package com.ws.dao;import com.ws.dao.User;import org.apache.ibatis.annotations.Param;publicinterface UserMapper {    publicvoidinsertUser(User user);    publicselectUser(@Param(value="userName") String userName);}
To add a mybatis configuration file to your project
    • To add a global configuration file Mybatis-config.xml

      Here are some things to be aware of, first of all, the DOCTYPE element, the following configuration corresponds to this is a management configuration, it is different from the configuration of the following mapper, you need to be aware of when copying XML files
      There is also a link configuration on the MySQL database, in the URL value if your own MySQL set is not allowed to remote access, then do not use the IP address directly, otherwise the operation will be error, it is written in localhost.
      The mappers element corresponds to the configuration of the rear mapper interface, do not forget to add, otherwise the runtime will also error.

<?xmlVersion= "1.0" encoding= "UTF-8"?><! DOCTYPEConfiguration Public "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd">  <configuration>    <environmentsdefault="Development">        <environmentid="Development">            <transactionmanagertype="JDBC" />            <datasourcetype="Unpooled">                <propertyname="Driver"value="Com.mysql.jdbc.Driver" />                <propertyname="url"value="Jdbc:mysql://localhost:3306/test" />                <propertyname="username"value="Root" />                <propertyname="Password"value="123456" />            </dataSource>        </environment>    </environments>    <mappers>        <mapperresource="User.xml" />    </mappers></configuration>
    • Add Mapper configuration file User.xml
<?xmlVersion= "1.0" encoding= "UTF-8"?><! DOCTYPEMapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="Com.ws.dao.UserMapper">    <selectid="Selectuser"resulttype="Com.ws.dao.User">SELECT * from user where username=#{username}</select>            <insertid="Insertuser"parametertype="Com.ws.dao.User">Insert into User (username,userpwd,usermail,useraddress) VALUES (#{username},#{userpwd},#{usermail},#{useradd Ress})</insert></mapper> 
Code for the main program
Package Com.ws.LearnMybatis;import Java.io.Reader;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 Com.ws.dao.User;import Com.ws.dao.UserMapper;/*** Hello world! * */ Public classApp { Public Static void Main(string[] args) {sqlsession session=NULL;Try{String resource="Mybatis-config.xml"; Reader reader=NULL; Reader=resources.Getresourceasreader(Resource); Sqlsessionfactory sqlsessionfactory=New Sqlsessionfactorybuilder().Build(reader); Session=sqlsessionfactory.opensession(); Usermapper usermapper=session.Getmapper(Usermapper.class); User User=usermapper.Selectuser("Xiaohong"); Session.Commit(); System. out.println(User.toString()); User=New User("Xiaogang","11111","[email protected]","Shanghai"); Usermapper.Insertuser(user); Session.Commit(); }Catch(Exception e) {e.Printstacktrace(); }finally{session.Close(); }            }}
    • Program Run Results

Use of Eclipse Maven MyBatis

Related Article

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.