欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Intellij Mybatis连接Mysql数据库

程序员文章站 2024-03-13 00:00:21
有好一段时间没有学习新东西了,过了个十月一感觉事情还是越早做越好,废话不多说了,说一下用mybatis连接mysql数据库,上一篇写了jdbc测试,我的数据库表还是原来的。...

有好一段时间没有学习新东西了,过了个十月一感觉事情还是越早做越好,废话不多说了,说一下用mybatis连接mysql数据库,上一篇写了jdbc测试,我的数据库表还是原来的。由于从网上查的资料大部分都是eclipse,由于我是做android开发的,习惯了ide,所以就硬着头皮来了,中间其实遇到过好多问题。

看一下工程结构

Intellij Mybatis连接Mysql数据库

先说一下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: 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

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 指定单条记录返回的类型-->
<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: 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

<?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数据库,希望对大家有所帮助