此博非教学,仅仅为本人的学习记录,望酌情参考,环境是Centos的eclipse企业版,自带jee和maven,所以下了个springboot插件,关于开始创建项目的时候遇到一点坑,应对的方法是出问题了就删除/root/.m2/目录下的那个文件夹,在这个目录下还可以创个settings.xml文件配置maven的阿里源,不然创一个项目能等半年。。。内容如下
<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd"> <mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> <mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/repositories/central/</url> </mirror> </mirrors> </settings>
之后进入eclipse里点windows然后preferences,选择maven,然后user setting,把刚刚那个路径写上去,apply并且close,完事。
接下来正题,创好项目后,我的理解是,所有的java文件夹都在demo下面,也就是在Application.java的同一级。
接触的第一个是用mybatis连接我的mysql,在demo下创建一个entity文件夹,新建一个实体类我这里是User,
package com.example.demo.entity; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Component; import lombok.Data; import lombok.Getter; import lombok.Setter; @Data @Getter @Setter @Component public class User { private String name; private String pass; private String mac1; private String mac2; private String mac3; public String getname() { return this.name; } public String getpass() { return this.pass; } public List<String> getmac() { List<String> maclist=new ArrayList(); maclist.add(mac1); maclist.add(mac2); maclist.add(mac3); return maclist; } }
然后在resources目录下的application.properties,编辑它内容为,最后两行可以不要,我用的注释方法,所以不用配置mapper的xml
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/Program?user=zhurui&password=Zhurui@111 mybatis.mapper-locations=classpath:mapper/*.xml mybatis.configuration.map-underscore-to-camel-case=true
然后demo下创建一个mapper文件夹,下面新建一个UserMapper.java,内容:(用的注释方法)
package com.cun.mapper; import org.apache.ibatis.annotations.Mapper; import org.mybatis.spring.annotation.MapperScan; import com.example.demo.entity.User; import org.apache.ibatis.annotations.Select; @Mapper public interface UserMapper { //1、查询数据 @Select("select * from user where name=#{name}") User getUser(String username) throws Exception; }
接着在在demo下创建service文件夹,创建相应的接口类UserService.java
package com.example.demo.service; import org.springframework.stereotype.Service; import com.example.demo.entity.User; //@Service public interface UserService { public User getUser(String name)throws Exception; }
然后是接口对应的实现类,也是demo下创ServiceImpl文件夹,创建UserServiceImpl.java,内容
package com.example.demo.serviceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.cun.mapper.UserMapper; import com.example.demo.entity.User; import com.example.demo.service.UserService; @Service public class UserSeviceImpl implements UserService{ @Autowired private UserMapper mapper; @Override public User getUser(String name) throws Exception { return mapper.getUser(name); } }
,最后是控制类在demo下创建controller文件夹,创建Usercontroller.java
package com.example.demo.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.example.demo.entity.User; import com.example.demo.service.UserService; @Controller public class Usercontroller { @Autowired private UserService service; @Autowired private User user; @RequestMapping(value="/login") @ResponseBody public User showlogin() throws Exception { user=service.getUser("zhurui"); System.out.print(user); return user; } }
到此,修改一下Application.java里的内容
package com.example.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @MapperScan("com.cun.mapper") @SpringBootApplication public class FirstProgramApplication { public static void main(String[] args) { SpringApplication.run(FirstProgramApplication.class, args); } }
也就加了一个注释,噢耶,终于成功了,但是我还是个大彩笔,中间配置pom的过程略了,等我把这些内容消化一下,总结个springboot的工作模式,然后再继续学习把。