IntelliJ mybatis connection MySQL database _java

Source: Internet
Author: User

There is a good period of time did not learn new things, after a October feeling things or the sooner do the better, nonsense not to say, to say that with MyBatis connection MySQL database, the previous write JDBC test, my database table or original. Because most of the data from the Internet is eclipse, because I am doing Android development, accustomed to the IDE, so I have to bite the bullet, in fact, encountered many problems in the middle.

Take a look at the engineering structure

First of all, the Java code, DAO is the query interface, model is Ben and the corresponding query statements of XML, I feel that this is a bit bad, User.xml put in the DAO inside better, and so understanding of the know, because I am a beginner so many things are not very understanding, later will give a reasonable package definition

Test is what I do for tests.

Tell me again. resources,configuration is a configuration file, log4j is log printing, others are temporarily unavailable.

Okay, so take a look at the code and I'll give it to you in the order of the files.
First you need to have an object that is built from the database

User.java

Package Com.fanyafeng.model;
Import Java.util.Date; /** * Author:fanyafeng * DATA:16/10/11 14:56 * email:fanyafeng@live.cn/public class User {private int id; private S
Tring username;
Private Date birthday;
Private String sex;
Private String address; public int getId () {return id;} public void setId (int id) {this.id = ID;} public String GetUserName () {return Usernam
E public void Setusername (String username) {this.username = username;} public Date Getbirthday () {return birthday;} PU Blic void Setbirthday (Date birthday) {this.birthday = birthday;} public String Getsex () {return sex;} public void SetS 
Ex (String sex) {this.sex = sex} public string getaddress () {return address;} public void Setaddress (string address) {
this.address = address; @Override public String toString () {return "user{" + "id=" + ID + ", username= ' + username + ' \ ' +", birthday= "+ bi
Rthday + ", sex= '" + sex + ' + ' + ", address= '" + address + ' \ ' + '} '; }
}

Iuserdao.java

Package Com.fanyafeng.dao;
Import Com.fanyafeng.model.User;
Import java.util.List;
/**
* Author:fanyafeng *
DATA:16/10/11 14:55
* email:fanyafeng@live.cn/public
interface Iuserdao {public
list<user> queryuserbyname (String name);
Public User Selectuserbyid (int id);
public void Add ();
public void del (int id);
public void alter (int id);
}

User.xml

 <?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" > <!-- RESULTTYPE Specifies the type returned by a single record--> <mapper namespace= "Com.fanyafeng.dao.IUserDao" > <select id= "Selectuserbyid" parametertype= "int" resulttype= "Com.fanyafeng.model.User" > select * from User where id = #{id} </select> <!-- '%${value} ' is a concatenation of SQL strings, only using value, which may cause SQL injection--> <select id= "Queryuserbyname" parametertype= "String" Resulttype= "Com.fanyafeng.model.User" > SELECT * from User WHERE username like #{name} </select> <!--INSERT in To USER (id,username,sex,address) VALUE (null, "Chen Yi-jin", "female", "college classmate")--> <insert id= "Add" parametertype= " Com.fanyafeng.model.User "> INSERT into User (id,username,birthday,sex,address) VALUES (#{id},#{username},#{ Birthday},#{sex},#{address}) </insert> </mapper> 

Usertest.java

Package com.fanyafeng.test;
Import Com.fanyafeng.model.User;
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 java.io.IOException;
Import Java.io.Reader;
Import Java.util.Date;
Import java.util.List; /** * Author:fanyafeng * DATA:16/10/11 14:58 * email:fanyafeng@live.cn/public class Usertest {private static sqlses
Sionfactory sqlsessionfactory;
private static reader reader; Static {try {reader = Resources.getresourceasreader ("Configuration.xml"); sqlsessionfactory = new
Sqlsessionfactorybuilder (). build (reader);
catch (IOException e) {e.printstacktrace ();}} public static void Main (string[] args) {sqlsession sqlsession = sqlsessionfactory.opensession (); try {//user user = (Us
ER) sqlsession.selectone ("Com.fanyafeng.dao.IUserDao.selectUserById", 2); System.out.println (user.getaddress () + user.getsex () + User.getbirthday());
System.out.println (User.getid () + user.getusername ());
List<user> userlist = sqlsession.selectlist ("Com.fanyafeng.dao.IUserDao.queryUserByName", "% Li ning%");
for (int i = 0; i < userlist.size (); i++) {System.out.println (Userlist.get (i). toString ());}
User user = new user ();
User.setid (100);
User.setbirthday (New Date ());
User.setusername ("Li Ning");
User.setsex ("female");
User.setaddress ("Home squatting");
int isadd = Sqlsession.insert ("Com.fanyafeng.dao.IUserDao.add", user);
Sqlsession.commit ()//Don't Forget} finally {Sqlsession.close ();}} }

Configration.xml

<?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> <!--If configured in Mapper, you can omit the configuration--> <!--<typeAliases>--> <!--<typealias alias= "User" type= "Com.fanyafeng.model.User"/>--> <!--</typeAliases>--> <!--and Spring integration Environments configuration will abolish--> <environments default= "Development" > <environment id= "Development" > <!-- Using JDBC Transaction management--> <transactionmanager type= "JDBC"/> <!--database connection pool--> <datasource type= "Pooled" > < Property name= "Driver" value= "Com.mysql.jdbc.Driver"/> <property name= "url" value= "Jdbc:mysql://localhost : 3306/mybatis?characterencoding=utf-8 "/> <property name=" username "value=" "root"/> <property name= " Password "value=" "/> </dataSource> </environment> </environments> <mappers> <mapper Resource= "Com/fanyafeng/model/user.xml"/> &LT;/mappers> </configuration> 

log4j words everybody according to own hobby to add is good, code in the comments written in detail, I will not elaborate, here is a place to let me very much, asked a big God just know what happened, the screenshot has the target directory, this is the compilation of good file storage location, but, the problem, Java file for me to compile a good place, but the resource XML file did not put to me, threw out an exception, looked up a lot of data, and then solve the problem
Look at the Pom.xml file, I changed it and added the build tag.

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > <modelversion >4.0.0</modelVersion> <groupId>com.fanyafeng</groupId> <artifactid>mybatisdemo</ artifactid> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name> Mybatisdemo Maven webapp</name> <url>http://maven.apache.org</url> <build> <finalname >mybatisdemo</finalName> <resources> <resource> <directory>src/main/resources</ directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include > </includes> </resource> <resource> <directory>src/main/java</directory> < includes> <include>**/*.xml</include> </includes> </resource> </resources> &Lt;/build> <dependencies> <dependency> <groupId>junit</groupId> <artifactid>junit </artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <! --Https://mvnrepository.com/artifact/mysql/mysql-connector-java--> <dependency> <groupid>mysql
</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!--https://mvnrepository.com/artifact/org.mybatis/mybatis--> <dependency> < Groupid>org.mybatis</groupid> <artifactId>mybatis</artifactId> <version>3.2.8</ Version> </dependency> <!--https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12--> < Dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> < Version>1.7.21</version> </dependency> <!--https://mvnrepository.com/artifact/org.slf4j/slf4j-api--> <dependency> <groupId>org.slf4j</groupId> <artifactid>slf4j-api</ Artifactid> <version>1.7.21</version> </dependency> <!--https://mvnrepository.com/ artifact/log4j/log4j--> <dependency> <groupId>log4j</groupId> <artifactid>log4j</ Artifactid> <version>1.2.17</version> </dependency> <!--https://mvnrepository.com/ Artifact/org.apache.logging.log4j/log4j-core--> <dependency> <groupid>org.apache.logging.log4j </groupId> <artifactId>log4j-core</artifactId> <version>2.5</version> </ dependency> <!--https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api--> <dependency > <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> < Version>2.5</version> </dependency> <!--https://mvnrepository.com/artifact/org.javassist/ Javassist;
<dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.18.1-GA</version> </dependency> <!--https://mvnrepository.com/artifact/ Commons-logging/commons-logging--> <dependency> <groupId>commons-logging</groupId> < Artifactid>commons-logging</artifactid> <version>1.1.1</version> </dependency> <!-- Https://mvnrepository.com/artifact/cglib/cglib--> <dependency> <groupId>cglib</groupId> < Artifactid>cglib</artifactid> <version>2.2.2</version> </dependency> <!--https:// Mvnrepository.com/artifact/asm/asm--> <dependency> <groupId>asm</groupId> <artifactId> asm</artifactid> <version>3.3.1</version> </dependency> </dependencies> </project >

This can be, I have achieved a check, increase, the other is learning.

The above is a small set to introduce the IntelliJ mybatis connection MySQL database, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.