Intellij Mybatis銜接Mysql數據庫。本站提示廣大學習愛好者:(Intellij Mybatis銜接Mysql數據庫)文章只能為提供參考,不一定能成為您想要的結果。以下是Intellij Mybatis銜接Mysql數據庫正文
有好一段時光沒有進修新器械了,過了個十月一感到工作照樣越早做越好,空話不多說了,說一下用mybatis銜接MySQL數據庫,上一篇寫了jdbc測試,我的數據庫表照樣本來的。因為從網上查的材料年夜部門都是eclipse,因為我是做Android開辟的,習氣了ide,所以就硬著頭皮來了,中央其實碰到過很多多少成績。
看一下工程構造
先說一下Java代碼,dao是查詢接口,model是ben和對應的查詢語句的xml,我感到如許有點欠好,user.xml放在dao外面比擬好,等懂得了就曉得了,因為我是初學者所以很多多少器械都不是很懂得,前期會給出公道的包的界說
test是我做測試用的
再說一下resources,configuration是設置裝備擺設文件,log4j是日記打印,其他的臨時用不到。
好來看一下代碼,我依照建文件的次序給年夜家來,
起首須要有一個對象,對象依據數據庫建
user.java
package com.fanyafeng.model; import java.util.Date; /** * Author: fanyafeng * Data: 16/10/11 14:56 * Email: [email protected] */ public class User { private int id; private String 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 username; } public void setUsername(String username) { this.username = username; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(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=" + birthday + ", 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: [email protected] */ 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 指訂單筆記錄前往的類型--> <mapper namespace="com.fanyafeng.dao.IUserDao"> <select id="selectUserById" parameterType="int" resultType="com.fanyafeng.model.User"> select * from user where id = #{id} </select> <!--'%${value}' 表現拼接sql串,只能應用value,有能夠會惹起sql注入的情形--> <select id="queryUserByName" parameterType="String" resultType="com.fanyafeng.model.User"> SELECT * FROM user WHERE username LIKE #{name} </select> <!--INSERT INTO USER (id,username,sex,address) VALUE (null,"陳潇然","女","年夜學同窗")--> <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: [email protected] */ public class UserTest { private static SqlSessionFactory 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 = (User) 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", "%李寧%"); 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("李寧"); user.setSex("女"); user.setAddress("家裡蹲"); int isAdd = sqlSession.insert("com.fanyafeng.dao.IUserDao.add", user); sqlSession.commit();//不要忘卻 } 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> <!--假如在mapper中停止設置裝備擺設了,此處可以省略設置裝備擺設--> <!--<typeAliases>--> <!--<typeAlias alias="User" type="com.fanyafeng.model.User"/>--> <!--</typeAliases>--> <!-- 和spring整合後 environments設置裝備擺設將破除--> <environments default="development"> <environment id="development"> <!-- 應用jdbc事務治理--> <transactionManager type="JDBC"/> <!-- 數據庫銜接池--> <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"/> </mappers> </configuration>
log4j的話年夜家依據本身的喜好添加就行了,代碼中正文寫的很具體了,我就不細講了,這裡有個處所讓我很憋屈,問了個年夜神才曉得咋回事,截圖中有target目次吧,這是編譯好文件的寄存地位,可是,成績來了,java文件給我編譯好放在哪裡了,可長短resource得xml文件沒有給我放出來,丟出來了一個異常,查了很多多少材料答非所問,然後是如許處理的
看一下pom.xml文件,我更改後的,加了build標簽中了
<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> </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>
這便可以了,我這裡完成了查,增,其余正在進修中。
以上所述是小編給年夜家引見的Intellij Mybatis銜接Mysql數據庫,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對網站的支撐!