Mybatis配置Mysql(JAVA)

Mybatis配置Mysql
配置文件
Conf.xml







    

        

            

            

            

                //在mysql 8.0.11以后的版本需要加cj标签,否则会连接失败
                

                

                

                

                

                

                

                

                

                

                

            

        

    

    

        

        

    


Mapper.xml:放在conf同一层目录







    

        CREATE TABLE  IF NOT EXISTS operate_message  (

        messageId INTEGER  AUTO_INCREMENT PRIMARY KEY,

        clusterId varchar(40) not null,

        mainMessage varchar(512) not null,

        operation varchar(40) not null,

        remark varchar(40) not null,

        time varchar(40)

        ) ENGINE=InnoDB AUTO_INCREMENT=43 DEFAULT CHARSET=utf8;

    

    

        INSERT INTO operate_message (clusterId,mainMessage,operation,remark,time)

                    VALUES  (#{clusterId},#{mainMessage},#{operation},#{remark},#{time});

    

    

    

    




代码

 

import io.OperateMessage;

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.List;

public class Test1 {

    public static void main(String[] args) throws IOException {


        InputStream inStream = new FileInputStream("E:\\\\study\\\\JAVAtest\\\\mysql\\\\mysql_test1\\\\src\\\\main\\\\resources\\\\city-conf.xml");

        //这里是为了测试绝对路径

        //这里也可以用相对路径
        //使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
        //String resource = "city-conf.xml";//这里放在工程的resources目录下
        //InputStream is = Test1.class.getClassLoader().getResourceAsStream(resource);

        //构建sqlSession的工厂

        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inStream);

        //使用MyBatis提供的Resources类加载mybatis的配置文件(它也加载关联的映射文件)

        //Reader reader = Resources.getResourceAsReader(resource); 

        //构建sqlSession的工厂

        //SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);

        //创建能执行映射文件中sql的sqlSession

        SqlSession session = sessionFactory.openSession();

        /**

         * 映射sql的标识字符串,

         * userMapper是userMapper.xml文件中mapper标签的namespace属性的值,

         * getUser是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL

         */

//        String statement = "cityMapper.getCity";//映射sql的标识字符串

        String createOperateMessage = "cityMapper.createOperateMessage";//映射sql的标识字符串

        session.update(createOperateMessage);

        //执行查询返回一个唯一user对象的sql

        OperateMessage operateMessage = new OperateMessage();

        operateMessage.setClusterId("test");

        operateMessage.setOperation("test");

        operateMessage.setRemark("test");

        operateMessage.setMainMessage("test");

        operateMessage.setTime("test");

        session.insert("cityMapper.insert",operateMessage);

        session.commit();



        List objects = session.selectList("cityMapper.getAllMessage");

        System.out.println(objects.toString());


        session.close();


    }

} 




 
  

运行结果:

[LogMessage{messageId='1', clusterId='LOCALCLUSTER', mainMeesage='SERVICE-WATCHDOG-487d8e5bb5f0427594bcf0d417dbd3a3', operation='start', time='2018-08-13 05:08:39'}, LogMessage{messageId='2', clusterId='LOCALCLUSTER', mainMeesage='SERVICE-ZOOKEEPER-98542d7448e74f7fab10fc70aab97089', operation='start', time='2018-08-13 05:08:53'}, LogMessage{messageId='3', clusterId='LOCALCLUSTER', mainMeesage='SERVICE-ZOOKEEPER-98542d7448e74f7fab10fc70aab97089', operation='start', time='2018-08-13 20:08:52'}, LogMessage{messageId='4', clusterId='LOCALCLUSTER', mainMeesage='SERVICE-ZOOKEEPER-98542d7448e74f7fab10fc70aab97089', operation='start', time='2018-08-13 20:08:53'}, LogMessage{messageId='5', clusterId='test', mainMeesage='test', operation='test', time='test'}, LogMessage{messageId='6', clusterId='test', mainMeesage='test', operation='test', time='test'}, LogMessage{messageId='7', clusterId='test', mainMeesage='test', operation='test', time='test'}, LogMessage{messageId='8', clusterId='test', mainMeesage='test', operation='test', time='test'}]

你可能感兴趣的:(数据库)