IDEA+Springboot+Mybatis+MySql的整合

用了一下午和半个晚上才在Springboot中使用Mybatis成功连接MySql。百度上众多纷纭,一人一个做法,被安排了。。。出于此我写下最终成功的方法和配置。

1.新建Springboot Project 在web中勾选web初次不建议全选中(即快速创建Spring Boot项目)
2.在pox.xml中配置mysql-connector和 mybatis

<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.0</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.35</version>
		</dependency>

3.在src/main/resources/application.properties中配置数据库连接信息

#配置mapper所在地址
mybatis.mapper-locations=classpath:/com/wayy/mybatis/mapper/*.xml
#配置数据库连接信息
spring.datasource.url=jdbc:mysql://localhost:3306/questions(数据库名)?useUnicode=true&characterEncoding=UTF-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=MySql密码

在这里插入图片描述在这里插入图片描述
*4.新建GeneratorMapper.xml配置 蓝色为需要自动生成的bean

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


	<!--此处需要修改-->
	 <!--指定连接数据库的JDBC驱动包所在的位置-->
    <classPathEntry location="E:/JAVA WEB/jar包/mysql-connector-java-5.1.26/mysql-connector-java-5.1.26-bin.jar"></classPathEntry>
    <context id="tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true" /><!-- 是否取消注释 -->
            <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳 -->
        </commentGenerator>
        


		<!--此处需要修改-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/questions(数据库名)"
                        userId="root"
                        password="MySql密码">
        </jdbcConnection>





		<!--此处需要修改-->
        <!--生成model类 targetPaceage指定model类的包名 targetProject指定生成的model放在哪个工程下-->
        <javaModelGenerator targetPackage="com.wayy.mybatis.model" targetProject="src/main/java" >

            <property name="enableSubPackages" value="false"></property>
            <property name="trimStrings" value="false"></property>
        </javaModelGenerator>



		<!--此处需要修改-->
        <!--生成MyBaits的Mapper.xml文件 targetPackage指定mapper文件的包名 targetProject指定生成的mapper.xml放在哪个工程下-->
        <sqlMapGenerator targetPackage="com.wayy.mybatis.mapper" targetProject="src/main/java" >
            <property name="enableSubPackages" value="false"></property>
        </sqlMapGenerator>



		<!--此处需要修改-->
        <!--生成MyBatis的Mapper接口类文件 targetPackage指定mapper接口类的包名 targetProject指定生成的mapper接口放在哪个工程下&#45;&#45;-->
        <javaClientGenerator targetPackage="com.wayy.mybatis.mapper" targetProject="src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="false"></property>
        </javaClientGenerator>
	

	<!--此处需要修改-->
	<!--蓝色为需要自动生成的bean-->
    <table tableName="User"
           domainObjectName="User"
           enableCountByExample="false"
           enableUpdateByExample="false"
           enableDeleteByExample="false"
           enableSelectByExample="false"
           selectByExampleQueryId="false"/>
    </context>
</generatorConfiguration>

目录结构
*5.在pom.xml中配置自动生成代码的插件

<build>
		<plugins>
			<!--Mybatis自动生成插件-->
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.6</version>
				<configuration>
					<configurationFile>GeneratorMapper.xml</configurationFile>
					<verbose>true</verbose>
					<overwrite>true</overwrite>
				</configuration>
			</plugin>

		</plugins>
	</build>

*6.在IDEA右侧点mAVEN Poeject下的本项目名下的Plugins下的mybatis-generator下的mybatis-generator:generate 双击自动生成代码 控制爱输出build sucess
在这里插入图片描述
8.再生产的UserMapper加上@Mapper或者在MapScan()扫描
在这里插入图片描述
9.新建Userservice接口 UserServiceImpl

	public interface UserService {
    public List<User> getAllUser();
}
public interface UserService {
    public List<User> getAllUser();
}

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> getAllUser() {
        return userMapper.selectAllUser();
    }
}

10.在UserMapper.xml中添加

<select id="selectAllUser" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from user
  </select>

在这里插入图片描述
11.新建Controller

(1)@RestController
(2)public class MybatisController {
(3)    @Autowired
(4)    private UserService userService;
(5)    @GetMapping("/getUser")
(6)    public List<User> users(){
(7)        return userService.getAllUser();
(8)    }
(9)}

12.运行主函数 访问页面http://localhost:8080/getUser
可能会报错 报错原因:Mapper包下没有mapper.xml在这里插入图片描述
打开pom.xml

<build>
		<plugins>
			<!--Mybatis自动生成插件-->
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.6</version>
				<configuration>
					<configurationFile>GeneratorMapper.xml</configurationFile>
					<verbose>true</verbose>
					<overwrite>true</overwrite>
				</configuration>
			</plugin>

		</plugins>


		<!--加入如下-->
		<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.xml</include>
				</includes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>
						**/*.*
					</include>
				</includes>
			</resource>
			<resource>
				<directory>src/main/webapp</directory>
				<targetPath>META-INF/resources</targetPath>
				<includes>
					<include>**/*.*</include>
				</includes>
			</resource>
		</resources>

	</build>
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
作为AI领域的热门技术之一,人脸识别已经在很多领域得到了广泛的应用。本文将介绍如何利用SpringBoot、Vue和Mybatis等技术实现人脸识别功能。 1. 准备工作 在开始实现功能之前,我们需要准备一些必要的工具和素材: - 一台安装了Windows或Linux操作系统的电脑 - Java JDK 8以上版本 - IntelliJ IDEA或Eclipse等IDE - Vue CLI和Node.js - OpenCV库 - 一些人脸照片和人脸数据库 2. 搭建环境 首先,我们需要创建一个SpringBoot项目,并在pom.xml文件中添加MybatisMySQLSpringBoot Web等依赖: ``` <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 接着,我们需要使用Vue CLI创建一个Vue项目,并安装Vuetify UI库和Axios HTTP库: ``` $ vue create face-recognition-system $ cd face-recognition-system $ npm install vuetify axios --save ``` 3. 图片处理 在人脸识别功能中,我们需要对照片进行处理,从照片中提取出人脸信息。这一步可以使用OpenCV库实现。 首先,我们需要下载安装OpenCV库,并在Java项目中添加相关的依赖: ``` <dependency> <groupId>org.openpnp</groupId> <artifactId>opencv</artifactId> <version>4.5.2-0</version> </dependency> ``` 接着,我们可以使用OpenCV库中的一些函数来处理照片。例如,我们可以使用CascadeClassifier类来加载人脸检测模型,并使用imread函数来读取照片: ``` CascadeClassifier faceDetector = new CascadeClassifier("haarcascade_frontalface_default.xml"); Mat image = Imgcodecs.imread("path/to/image.jpg"); MatOfRect faceDetections = new MatOfRect(); faceDetector.detectMultiScale(image, faceDetections); ``` 4. 数据库操作 在人脸识别功能中,我们需要将从照片中提取出的人脸信息存储到数据库中,以便后续的识别和比对。 使用Mybatis操作数据库非常方便。我们只需要在Java项目中创建一个Mapper接口,定义相关的SQL语句,并使用@Mapper注解将接口注册为Mybatis的Mapper。例如,我们可以定义一个UserMapper接口用于操作用户信息的表: ``` @Mapper public interface UserMapper { @Select("select * from user where username=#{username}") User findByUsername(String username); @Select("select * from user where face_id=#{faceId}") User findByFaceId(String faceId); @Insert("insert into user(username, password, face_id) values(#{username}, #{password}, #{faceId})") int insert(User user); @Update("update user set username=#{username}, password=#{password}, face_id=#{faceId} where id=#{id}") int update(User user); @Delete("delete from user where id=#{id}") int deleteById(int id); } ``` 在使用Mapper接口中的方法之前,我们需要在application.properties中配置数据库信息: ``` spring.datasource.url=jdbc:mysql://localhost:3306/face_recognition_db spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 5. 实现识别和比对 最后,我们需要将人脸识别的功能整合起来,完成整个系统的实现。 首先,在前端页面中,我们可以使用Vuetify UI库中的<v-file-input>组件来上传照片,并使用Axios库将照片发送到后端的接口: ``` <v-file-input v-model="file" label="Choose a file"></v-file-input> methods: { uploadFile() { let formData = new FormData(); formData.append('file', this.file); axios.post('/api/recognition', formData, { headers: { 'Content-Type': 'multipart/form-data' } }).then(response => { this.result = response.data.result; }).catch(error => { console.error(error); }) } } ``` 接着,在后端的Controller中,我们可以使用OpenCV库和Mybatis库来进行照片识别和比对。例如,我们可以定义一个/recognition接口用于照片识别和比对: ``` @PostMapping("/recognition") public Result recognition(@RequestParam("file") MultipartFile file) throws IOException { CascadeClassifier faceDetector = new CascadeClassifier("haarcascade_frontalface_default.xml"); Mat image = Imgcodecs.imdecode(new MatOfByte(file.getBytes()), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED); MatOfRect faceDetections = new MatOfRect(); faceDetector.detectMultiScale(image, faceDetections); // 识别出的人脸数量不为1,则返回错误信息 if (faceDetections.toArray().length != 1) { return Result.error("No or multiple faces detected"); } Mat face = image.submat(faceDetections.toArray()[0]); byte[] faceBytes = new byte[(int) (face.total() * face.elemSize())]; face.get(0, 0, faceBytes); String faceId = FaceRecognitionUtils.getFaceId(faceBytes); // 根据faceId在数据库中查找对应的用户 User user = userMapper.findByFaceId(faceId); if (user == null) { return Result.error("Unknown user"); } return Result.success(user.getUsername()); } ``` 在上述代码中,我们首先使用OpenCV库识别照片中的人脸,然后使用FaceRecognitionUtils类中的getFaceId函数将人脸信息转化为一个唯一的faceId,最后查询数据库中是否存在对应的用户。 至此,整个人脸识别系统的实现已经完成了。当然,由于涉及到的技术非常多,上述代码也只是一个简单的示例。如果您想深入了解人脸识别相关的技术和应用,建议再深入学习一下相关的知识和技术。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值