首页 > 数据库 > 其他综合 >

IntellijMybatis连接Mysql数据库

2016-10-12

有好一段时间没有学习新东西了,过了个十月一感觉事情还是越早做越好,废话不多说了,说一下用mybatis连接mysql数据库,上一篇写了jdbc测试,我的数据库表还是原来的。IntellijMybatis连接Mysql数据库。

有好一段时间没有学习新东西了,过了个十月一感觉事情还是越早做越好,废话不多说了,说一下用mybatis连接mysql数据库,上一篇写了jdbc测试,我的数据库表还是原来的。由于从网上查的资料大部分都是eclipse,由于我是做android开发的,习惯了ide,所以就硬着头皮来了,中间其实遇到过好多问题。
看一下工程结构
这里写图片描述vc+6w6OstcjA7b3iwcu+zdaqtcDBy6Os08nT2s7Sysez9dGn1d/L+dLUusO24Larzve2vLK7yse63MDtveKjrLrzxtq74bj4s/a6z8DttcSw/LXEtqjS5TxiciAvPg0KdGVzdMrHztLX9rLiytTTw7XEPGJyIC8+DQrU2cu10rvPwnJlc291cmNlc6OsY29uZmlndXJhdGlvbsrHxeTWw87EvP6jrGxvZzRqysfI1da+tPLToaOsxuTL+7XE1N3KsdPDsru1vaGjPGJyIC8+DQq6w8C0v7TSu8/CtPrC66OsztKwtNXVvajOxLz+tcTLs9DyuPi087zSwLSjrDxiciAvPg0KytfPyNDo0qrT0NK7uPa21M/zo6y21M/zuPm+3cr9vt2/4r2oPGJyIC8+DQp1c2VyLmphdmE8L3A+DQo8cHJlIGNsYXNzPQ=="brush:sql;"> 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 queryUserByName(String name);

    public User selectUserById(int id);

    public void add();

    public void del(int id);

    public void alter(int id);

}

user.xml






    

    
    

    
    
        insert into user (id,username,birthday,sex,address) values (#{id},#{username},#{birthday},#{sex},#{address})
    

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 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



    
    
        
    


    
    
        
            
            
            
            
                
                
                
                
            
        
    

    
        
    


log4j的话大家根据自己的爱好添加就好了,代码中注释写的很详细了,我就不细讲了,这里有个地方让我很憋屈,问了个大神才知道咋回事,截图中有target目录吧,这是编译好文件的存放位置,可是,问题来了,java文件给我编译好放在哪里了,可是非resource得xml文件没有给我放进去,丢出来了一个异常,查了好多资料答非所问,然后是这样解决的
看一下pom.xml文件,我更改后的,加了build标签中了


    4.0.0
    com.fanyafeng
    mybatisdemo
    war
    1.0-SNAPSHOT
    mybatisdemo Maven Webapp
    http://maven.apache.org

    
        mybatisdemo

        
            
                src/main/resources
                
                    **/*.xml
                    **/*.properties
                
            
            
                src/main/java
                
                    **/*.xml
                
            
        
    

    
        
            junit
            junit
            3.8.1
            test
        

        
        
            mysql
            mysql-connector-java
            5.1.38
        

        
        
            org.mybatis
            mybatis
            3.2.8
        

        
        
            org.slf4j
            slf4j-log4j12
            1.7.21
        

        
        
            org.slf4j
            slf4j-api
            1.7.21
        

        
        
            log4j
            log4j
            1.2.17
        

        
        
            org.apache.logging.log4j
            log4j-core
            2.5
        

        
        
            org.apache.logging.log4j
            log4j-api
            2.5
        

        
        
            org.javassist
            javassist
            3.18.1-GA
        

        
        
            commons-logging
            commons-logging
            1.1.1
        

        
        
            cglib
            cglib
            2.2.2
        

        
        
            asm
            asm
            3.3.1
       

这就可以了,我这里实现了查,增,别的正在学习中。
写给自己:不开始永远不知道什么时候能结束

相关文章
最新文章
热点推荐