一. mysql 运行
1. 启动mysql (mac 系统)
bash mysql.server start
2. mysql -u root -p
输入密码
3. 就可以进入mysql 了。
4. 也可安装 My Sql Workbench 来运行,建表,查询等操作了。
二. spring boot java mybatis 使用
参考:https://www.jianshu.com/p/7d164b09c3e2
- 生成mysql 当中的数据库,及表
CREATE TABLE `mydb` ( `id` int(11) NOT NULL, `name` varchar(80) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
- 下载mybatis generator 。
- 生成 generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <!--数据库驱动--> <classPathEntry location="mysql-connector-java-8.0.11.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库链接地址账号密码--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/yangzm" userId="root" password="yzm"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--生成Model类存放位置--> <javaModelGenerator targetPackage="com.example.yzm.demo2" targetProject="src"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--生成映射文件存放位置--> <sqlMapGenerator targetPackage="com.example.yzm.demo2" targetProject="src"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!--生成Dao类存放位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.yzm.demo2" targetProject="src"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!--生成对应表及类名--> <table tableName="mydb" domainObjectName="mydb" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration>
-
命令行运行:
java -jar mybatis-generator-core-1.3.6.jar -configfile generatorConfig.xml -overwrite
这里会在下边生成一个“src”的文件夹里边是三个要用到的文件(mydb.java,mydbMapper.java,mydbMapper.xml)
-
加入到spring boot 工程里边。
1). 可以在生成的每个dao文件里添加@mapper注解@Mapper public interface mydbMapper { int deleteByPrimaryKey(Integer id); int insert(mydb record); int insertSelective(mydb record); mydb selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(mydb record); int updateByPrimaryKey(mydb record); }
2). 也可以在App类添加@MapperScan注解,如:
@MapperScan("com....") 这个很多文章都写了。
3). 配置mapper的路径:
在application.properties里加一行:mybatis.mapper-locations=classpath*:sqlxml/*Mapper.xml
sqlxml 是resource 下边的文件夹,把刚才生成的文件copy到里边,这样spring boot 就可以找到了。
ps:model的路径不用配置,在mapper文件里面已有
1.无论是classpath还是classpath*都可以加载整个classpath下(包括jar包里面)的资源文件。
2.classpath只会返回第一个匹配的资源,查找路径是优先在项目中存在资源文件,再查找jar包。
3.文件名字包含通配符资源(如果spring-*.xml,spring*.xml), 如果根目录为"", classpath加载不到任何资源, 而classpath*则可以加载到classpath中可以匹配的目录中的资源,但是不能加载到jar包中的资源
第1,2点比较好表理解,大家可以自行测试,第三点表述有点绕,举个例,现在有资源文件结构如下:
classpath:notice*.txt 加载不到资源
classpath*:notice*.txt 加载到resource根目录下notice.txt
classpath:META-INF/notice*.txt 加载到META-INF下的一个资源(classpath是加载到匹配的第一个资源,就算删除classpath下的notice.txt,他仍然可以 加载jar包中的notice.txt)
classpath:META-*/notice*.txt 加载不到任何资源
classpath*:META-INF/notice*.txt 加载到classpath以及所有jar包中META-INF目录下以notice开头的txt文件
classpath*:META-*/notice*.txt 只能加载到classpath下 META-INF目录的notice.txt -
工程中调用代码:
1).@Autowired com.example.yzm.demo2.mydbMapper mapper; 2).
// 我数据库里加了一条记录,这里的1是有数据的 mydb d = mapper.selectByPrimaryKey(1);