有好一段时间没有学习新东西了,过了个十月一感觉事情还是越早做越好,废话不多说了,说一下用mybatis连接MySQL数据库,上一篇写了jdbc测试,我的数据库表还是原来的。由于从网上查的资料大部分都是eclipse,由于我是做Android开发的,习惯了ide,所以就硬着头皮来了,中间其实遇到过好多问题。
看一下工程结构
先说一下Java代码,dao是查询接口,model是ben和对应的查询语句的xml,我感觉这样有点不好,user.xml放在dao里面比较好,等理解了就知道了,由于我是初学者所以好多东西都不是很理解,后期会给出合理的包的定义
test是我做测试用的
再说一下resources,configuration是配置文件,log4j是日志打印,其他的暂时用不到。
好来看一下代码,我按照建文件的顺序给大家来,
首先需要有一个对象,对象根据数据库建
user.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
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 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
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 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<?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标签中了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
<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数据库,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://blog.csdn.net/qq_23195583/article/details/52792199